
///////////////Analysis file for "How Much Should We Trust the Dictator's GDP Growth Estimates?" by Luis R. Martinez. last edited: 2021/12/15

/*

///////////////////////NOTES//////////////////////////////////

//Description:

This do-file carries out all the analyses in the paper and produces all tables and figures.

// Instructions:

*1. Fix global "dir" immediately below  to the destination folder
*2. Do-file called "setup" must be run before this one to create datasets for estimations and auxiliary map files 
*3. R script zam_v1.r in Do-files\ZAM folder must be run before this one to create the dataset ZAM_sample_out.dta used in Table D16 (path to temp folder must be adjusted in line 19 of R script)
*4. Do-file must be run in its entirety to produce Figure 3 plotting all the estimated sigmas
*5. Piecewise execution may also miss "if" conditions specified in locals before some tables or figures
*6. Destination folder must include following folders:
-"Data" w/ following subfolders:
	-"temp" which includes intermediate files with regression estimates produced during execution of do-file
		-"sigmas" which holds copies of each estimate of sigma used in Figure 3
		-"ctrys" which holds copies of each estimate removing one country
		-"ctrys2" which holds copies of each estimate removing two countries
	-"master" which includes the final datasets used in the analysis
-"Output" which includes the following subfolders:
	-"Figures", which includes all figures produced during execution of do-file in PDF format
	-"Tables", which includes all tables produced during execution of do-file in txt and tex format 
*7. In case of error due to missing ado files, run "setup" do-file uncommenting package installation at the very top
	
//	Computational Environment:

Operating System: Windows 10
Stata version: Stata/MP 17.0 for Windows (64 bit)
Estimated running time: 9 hours
	
//	Content of Do-file:

*1. Main tables (1-6): line 68
*2. Main figures (1-6): line 735
*3. Appendix tables (A1-D17): line 992
*4. Appendix figures (A1-D6): line 3391

/////////////////////////////////////////////////////////////

*/

clear all
set more off
global dir "xxx"	

global tables "${dir}\Output\Tables"
global figures "${dir}\Output\Figures"
global temp "${dir}\Data\temp"
global sigmas "${dir}\Data\temp\sigmas"

global seed 20051702	//seed used for bootstrap in estimation of sigma SE
global reps=1000	//number of repetitions for bootstrap

local i=1	// this local identifies each estimate of sigma

timer clear 1
timer on 1

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//////////////////// MAIN TABLES ////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////
////// Table 1: Main estimates of autocracy gradient of NTL elasticity of GDP////////
/////////////////////////////////////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

local out "outreg2 using "${tables}\table_1", bracket nocons nor2 keep(lndn* fiw* Dpfree Dnfree autocracyFH) dec(3)"

*column 1
reghdfe lngdp14 lndn13 if fiw!=., absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
*column 2
reghdfe lngdp14 lndn13 fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 3
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 fiw lndn13_fiw, absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(T1C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw lndn13_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 4
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), saving(${temp}\boot_T1C4,replace) cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(T1C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 5
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 Dpfree Dnfree lndn13_Dpfree lndn13_Dnfree, absorb(countrycode i.year) cluster(countrycode)
	return scalar sigma = _b[lndn13_Dnfree]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(T1C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 Dpfree Dnfree lndn13_Dpfree lndn13_Dnfree, absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 6
gen lndn13_autocracyFH=lndn13*autocracyFH
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 autocracyFH lndn13_autocracyFH, absorb(countrycode i.year) cluster(countrycode)
	return scalar sigma = _b[lndn13_autocracyFH]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(T1C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 autocracyFH lndn13_autocracyFH, absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 7
//I rename the long-run variables to facilitate visualization of the table
drop lndn13 fiw fiw2 lndn13_fiw
rename lndn13_lr lndn13 
rename fiw_lr fiw
gen fiw2=fiw^2 
gen lndn13_fiw=lndn13*fiw
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14_lr lndn13 fiw fiw2 lndn13_fiw if (year==1993|year==2013), absorb(countrycode i.year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(T1C7) erows(ci_percentile ci_bc)
reghdfe lngdp14_lr lndn13 fiw fiw2 lndn13_fiw if (year==1993|year==2013), absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)
local i=`i'+1

/////////////////////////////////////////////////////////////////////////////////
////// Table 2: Autocracy gradient of NTL elasticity of GDP subcomponents////////
/////////////////////////////////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

local out "outreg2 using "${tables}\table_2", bracket nocons nor2 keep(lndn* fiw* D*) dec(3)"
local cond_sub cons_gdp!=.&inv_gdp!=.&gov_gdp!=.&exp_gdp!=.&imp_gdp!=.

*column 1: consumption
capture program drop sigma_boot_sub
program define sigma_boot_sub, rclass
	args y cond
	reghdfe `y' lndn13 fiw fiw2 lndn13_fiw if `cond', absorb(countrycode i.year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_sub lncons `cond_sub' 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
reghdfe lncons lndn13 fiw fiw2 lndn13_fiw if `cond_sub', absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
*column 2: investment
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_sub lninv `cond_sub' 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
reghdfe lninv lndn13 fiw fiw2 lndn13_fiw if `cond_sub', absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) 
*column 3: gvt spending
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_sub lngov `cond_sub' 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
reghdfe lngov lndn13 fiw fiw2 lndn13_fiw if `cond_sub', absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) 
*column 4: exports
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_sub lnexp `cond_sub' 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
reghdfe lnexp lndn13 fiw fiw2 lndn13_fiw if `cond_sub', absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) 
*column 5: imports
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_sub lnimp `cond_sub' 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
reghdfe lnimp lndn13 fiw fiw2 lndn13_fiw if `cond_sub', absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)

/////////////////////////////////////////////////
////// Table 3: Heterogeneity: Low growth////////
/////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

*Create all interactions
gen lndn13_low=lndn13*low
gen low_fiw=low*fiw
gen lndn13_fiw_low=lndn13_fiw*low
gen low_Dnfree=low*Dnfree
gen lndn13_Dnfree_low=lndn13_Dnfree*low
gen low_Dpfree=low*Dpfree
gen lndn13_Dpfree_low=lndn13_Dpfree*low

local out "outreg2 using "${tables}\table_3", bracket nocons nor2 dec(3)"

*column 1: hetero low growth
reghdfe lngdp14 lndn13 low lndn13_low if fiw!=.,absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
*column 2: full spec + hetero low growth
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 low fiw fiw2 lndn13_low lndn13_fiw,absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(T3C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 low fiw fiw2 lndn13_low lndn13_fiw,absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 3: hetero low growth with FIW
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 low low_fiw lndn13_low lndn13_fiw lndn13_fiw_low,absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
	return scalar sigma1 = `iqr'*(_b[lndn13_fiw]+_b[lndn13_fiw_low])/(_b[lndn13]+_b[lndn13_low])
exit
end
bootstrap r(sigma) r(sigma1), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
local sigma1: di %4.3f a[1,2]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
local sigma1SE: di %4.3f sqrt(b[2,2])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local sigma1_95_low: di %3.2f c[1,2]
local sigma1_95_high: di %3.2f c[2,2]
reghdfe lngdp14 lndn13 fiw fiw2 low low_fiw lndn13_low lndn13_fiw lndn13_fiw_low,absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma_high,`sigma', sigma_high SE, [`sigmaSE'], sigma_high 95 CI, [`sigma_95_low' `sigma_95_high'], sigma_low,`sigma1', sigma_low SE, [`sigma1SE'], sigma_low 95 CI, [`sigma1_95_low' `sigma1_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 4: full spec discrete + hetero low growth
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 Dpfree Dnfree low lndn13_low lndn13_Dpfree lndn13_Dnfree, absorb(countrycode i.year) cluster(countrycode)
	return scalar sigma = _b[lndn13_Dnfree]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(T3C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 Dpfree Dnfree low lndn13_low lndn13_Dpfree lndn13_Dnfree, absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 5: hetero low growth with FIW discrete
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 Dpfree Dnfree low low_Dpfree low_Dnfree lndn13_low lndn13_Dpfree lndn13_Dnfree lndn13_Dpfree_low lndn13_Dnfree_low, absorb(countrycode year) cluster(countrycode)
	return scalar sigma = _b[lndn13_Dnfree]/_b[lndn13]
	return scalar sigma1 = (_b[lndn13_Dnfree]+_b[lndn13_Dnfree_low])/(_b[lndn13]+_b[lndn13_low])
exit
end
bootstrap r(sigma) r(sigma1), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
local sigma1: di %4.3f a[1,2]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
local sigma1SE: di %4.3f sqrt(b[2,2])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local sigma1_95_low: di %3.2f c[1,2]
local sigma1_95_high: di %3.2f c[2,2]
reghdfe lngdp14 lndn13 Dpfree Dnfree low low_Dpfree low_Dnfree lndn13_low lndn13_Dpfree lndn13_Dnfree lndn13_Dpfree_low lndn13_Dnfree_low, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma_high,`sigma', sigma_high SE, [`sigmaSE'], sigma_high 95 CI, [`sigma_95_low' `sigma_95_high'], sigma_low,`sigma1', sigma_low SE, [`sigma1SE'], sigma_low 95 CI, [`sigma1_95_low' `sigma1_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)

//////////////////////////////////////////////////////////////////////
////// Tables 4 (A7) and Figure 5: Heterogeneity: IDA crossing////////
//////////////////////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

//variables for analysis

gen lndn13_gni=lndn13*gni_pc
gen lndn13_cross=lndn13*ida_cross_post
gen fiw_cross=fiw*ida_cross_post
gen lndn13_fiw_cross =lndn13_fiw*ida_cross_post

local out "outreg2 using "${tables}\table_4", bracket nocons nor2 dec(3)"
local cond_ida fiw!=.&ida_ctry==1&gni_pc!=.

//Tables 4 (main results) and A7 (full results)  

*column1: basic correlation
reghdfe lngdp14 lndn13 if `cond_ida', absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
*column2: autocracy gradient in NTL elasticity of GDP
capture program drop sigma_boot
program define sigma_boot, rclass
	args cond
	reghdfe lngdp14 fiw fiw2 lndn13 lndn13_fiw if `cond', absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot `cond_ida'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(T4C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 fiw fiw2 lndn13 lndn13_fiw if `cond_ida', absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 3: change in elasticity after crossing
reghdfe lngdp14 ida_cross_post lndn13 lndn13_cross if `cond_ida', absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 4: same as column 3 but controlling for GNI pc (i.e. running var)
reghdfe lngdp14  ida_cross_post gni_pc lndn13 lndn13_cross lndn13_gni if `cond_ida', absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 5: full spec
capture program drop sigma_boot
program define sigma_boot, rclass
	args cond
	reghdfe lngdp14 fiw fiw2 ida_cross_post fiw_cross gni_pc lndn13 lndn13_fiw lndn13_cross lndn13_fiw_cross lndn13_gni if `cond', absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
	return scalar sigma1 = `iqr'*(_b[lndn13_fiw]+_b[lndn13_fiw_cross])/(_b[lndn13]+_b[lndn13_cross])
exit
end
bootstrap r(sigma) r(sigma1), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot `cond_ida'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
local sigma1: di %4.3f a[1,2]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
local sigma1SE: di %4.3f sqrt(b[2,2])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local sigma1_95_low: di %3.2f c[1,2]
local sigma1_95_high: di %3.2f c[2,2]
reghdfe lngdp14 fiw fiw2 ida_cross_post fiw_cross gni_pc lndn13 lndn13_fiw lndn13_cross lndn13_fiw_cross lndn13_gni if `cond_ida', absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma_pre,`sigma', sigma_pre SE, [`sigmaSE'], sigma_pre 95 CI, [`sigma_95_low' `sigma_95_high'], sigma_post,`sigma1', sigma_post SE, [`sigma1SE'], sigma_post 95 CI, [`sigma1_95_low' `sigma1_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 6: full spec + controlling for sh_unlit
gen sh_unlit= count_unlit/count_lights
gen lndn13_sh_unlit=lndn13*sh_unlit
capture program drop sigma_boot
program define sigma_boot, rclass
	args cond
	reghdfe lngdp14 fiw fiw2 sh_unlit ida_cross_post fiw_cross gni_pc lndn13 lndn13_fiw lndn13_cross lndn13_fiw_cross lndn13_gni lndn13_sh_unlit if `cond', absorb(countrycode year) cluster(countrycode)
	sum sh_unlit if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = (`iqr'*_b[lndn13_fiw])/(_b[lndn13]+(`x_mean'*_b[lndn13_sh_unlit]))
	return scalar sigma1 = (`iqr'*(_b[lndn13_fiw]+_b[lndn13_fiw_cross]))/(_b[lndn13]+_b[lndn13_cross]+(`x_mean'*_b[lndn13_sh_unlit]))
exit
end
bootstrap r(sigma) r(sigma1), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot `cond_ida'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
local sigma1: di %4.3f a[1,2]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
local sigma1SE: di %4.3f sqrt(b[2,2])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local sigma1_95_low: di %3.2f c[1,2]
local sigma1_95_high: di %3.2f c[2,2]
reghdfe lngdp14 fiw fiw2 sh_unlit ida_cross_post fiw_cross gni_pc lndn13 lndn13_fiw lndn13_cross lndn13_fiw_cross lndn13_gni lndn13_sh_unlit if `cond_ida', absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma_pre,`sigma', sigma_pre SE, [`sigmaSE'], sigma_pre 95 CI, [`sigma_95_low' `sigma_95_high'], sigma_post,`sigma1', sigma_post SE, [`sigma1SE'], sigma_post 95 CI, [`sigma1_95_low' `sigma1_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)

//Figure 5: Event study

*create leads and lags
sort countrycode year
list countryname year if ida_cross==1	//Table A4
gen ida_cross_lag0=ida_cross==1&ida_cross[_n-1]==0&countrycode==countrycode[_n-1]&year==year[_n-1]+1&ida_ctry==1
forvalues x=1/25{
	local y=`x'-1
	gen ida_cross_lag`x'=ida_cross_lag`y'==0&ida_cross_lag`y'[_n-1]==1&countrycode==countrycode[_n-1]&year==year[_n-1]+1
}
gen ida_cross_lead1=ida_cross[_n+1]==1&ida_cross==0&countrycode==countrycode[_n+1]&year==year[_n+1]-1&ida_ctry==1
forvalues x=2/25{	
	local y=`x'-1
	gen ida_cross_lead`x'=ida_cross_lead`y'==0&ida_cross_lead`y'[_n+1]==1&countrycode==countrycode[_n+1]&year==year[_n+1]-1
}
*aggregate endpoints
egen ida_cross_lag7p=rowtotal(ida_cross_lag7 - ida_cross_lag25)
egen ida_cross_lead7p=rowtotal(ida_cross_lead7 - ida_cross_lead25)
*interact with lights
foreach x of varlist ida_cross_lead7p ida_cross_lead6 ida_cross_lead5 ida_cross_lead4 ida_cross_lead3 ida_cross_lead2 ida_cross_lead1 ida_cross_lag0 ida_cross_lag1 ida_cross_lag2 ida_cross_lag3 ida_cross_lag4 ida_cross_lag5 ida_cross_lag6 ida_cross_lag7p{
	gen lndn13_`x'=lndn13*`x'
}
*interact with FIW
foreach x of varlist ida_cross_lead7p ida_cross_lead6 ida_cross_lead5 ida_cross_lead4 ida_cross_lead3 ida_cross_lead2 ida_cross_lead1 ida_cross_lag0 ida_cross_lag1 ida_cross_lag2 ida_cross_lag3 ida_cross_lag4 ida_cross_lag5 ida_cross_lag6 ida_cross_lag7p{
	gen fiw_`x'=fiw*`x'
}
*interact with lights x FIW
foreach x of varlist ida_cross_lead7p ida_cross_lead6 ida_cross_lead5 ida_cross_lead4 ida_cross_lead3 ida_cross_lead2 ida_cross_lead1 ida_cross_lag0 ida_cross_lag1 ida_cross_lag2 ida_cross_lag3 ida_cross_lag4 ida_cross_lag5 ida_cross_lag6 ida_cross_lag7p{
	gen lndn13_fiw_`x'=lndn13_fiw*`x'
}
*create FIW and lights interaction for non-crossers
egen temp=max(ida_cross),by(countrycode)
gen no_cross=1-temp
drop temp
gen fiw_other=cond(no_cross==1,fiw,0)
gen lndn13_fiw_other=lndn13*fiw_other
*regression
reghdfe lngdp14 gni_pc fiw2 ida_cross_lead7p ida_cross_lead6 ida_cross_lead5 ida_cross_lead4 ida_cross_lead3 ida_cross_lead2 ida_cross_lag0 ida_cross_lag1 ida_cross_lag2 ida_cross_lag3 ida_cross_lag4 ida_cross_lag5 ida_cross_lag6 ida_cross_lag7p fiw_ida_cross_lead7p-fiw_ida_cross_lag7p lndn13 lndn13_gni lndn13_ida_cross_lead7p-lndn13_ida_cross_lag7p lndn13_fiw_ida_cross_lead7p-lndn13_fiw_ida_cross_lag7p fiw_other lndn13_fiw_other if `cond_ida', absorb(countrycode year) cluster(countrycode) nocons
parmest,saving(${temp}\ida_event,replace)
preserve
	use ${dir}\Data\temp\ida_event,clear
	keep in 50/62
	gen id=0 in 7
	replace id=id[_n-1]+1 in 8/13
	replace id=[_n]-7 in 1/6
	sort id
	twoway (scatter estimate id,mcolor(gs8) msymbol(diamond)) (rcap min95 max95 id), ytitle(NTL elasticity of GDP: FIW gradient) yline(0) ylabel(, angle(horizontal) format(%3.2f)) xtitle(Years since crossing IDA threshold) xlabel(-6(2)6) xline(0, lpattern(dash)) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(event_ida,replace)
	graph export "${figures}\figure_5.eps", as(eps) replace
restore

////////////////////////////////////////////////////
////// Table 5: Application - Aid and growth////////
////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

*variable creation
sort countrycode year
gen oda_gni_in=cond(dt_oda_odat_gn_zs<0,0,dt_oda_odat_gn_zs)	//set negatives (contributors) to zero aid/GNI
gen ln_oda_gni_in=ln(1+oda_gni_in)
disp (0.008+0.023)/(0.230+0.013) //bias estimate in IDA sub-sample (T5C5)
gen factor=1/(1+(fiw*0.128))
gen growth_adj=gdp14_growth*factor
gen gdp_adj=ny_gdp_mktp_kn_2014_11 if ida_cross_post==0
replace gdp_adj=gdp_adj[_n-1]*(1+growth_adj) if countrycode==countrycode[_n-1]&gdp_adj==. // I replace post-cross adjusted GDP 
gen lngdp_adj=ln(gdp_adj)
gen gdp_pc_adj=gdp_adj/sp_pop_totl_2014_11
gen ln_gdp_pc=ln(ny_gdp_pcap_kn_2014_11) 
gen ln_gdp_pc_adj=ln(gdp_pc_adj)
*first-differences of raw and adjusted GDP per capita
gen d_ln_gdp_pc=ln_gdp_pc-ln_gdp_pc[_n-1] if countrycode==countrycode[_n-1]
gen d_ln_gdp_pc_adj=ln_gdp_pc_adj-ln_gdp_pc_adj[_n-1] if countrycode==countrycode[_n-1]

local out "outreg2 using "${tables}\table_5", bracket nocons nor2 dec(3) keep(ida_cross_post gni_pc)"

//I drop Montenegro, Serbia and Kosovo to keep sample constant across columns (no impact on results)
local if if countrycode!="MNE"&countrycode!="SRB"&countrycode!="XKX"&ida_ctry==1&year>=1992&year<=2013&fiw!=.&gni_pc!=.&ln_gdp_pc_adj!=.&ln_oda_gni_in!=.

*column 1: ODA
reghdfe ln_oda_gni_in ida_cross_post gni_pc i.year `if',absorb(countrycode) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
gen temp=e(sample)
*column 2: ln GDP pc (naive)
reghdfe ln_gdp_pc ida_cross_post gni_pc c.year#i.isonv10 i.year `if',absorb(countrycode) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within))
gen temp1=e(sample)
*column 3: ln GDP pc (adj)
reghdfe ln_gdp_pc_adj ida_cross_post gni_pc c.year#i.isonv10 i.year `if',absorb(countrycode) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 4: d_ln GDP pc (naive)
reghdfe d_ln_gdp_pc ida_cross_post gni_pc  i.year `if',absorb(countrycode) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 5: d_ln GDP pc (adj)
reghdfe d_ln_gdp_pc_adj ida_cross_post gni_pc i.year `if',absorb(countrycode) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)

//////////////////////////////////////////////////
////// Tables 6 (A8) and Figure A16: SDDS ////////
//////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

//vars for analysis
egen sdds_ctry13=max(sdds_met) if year<=2013,by(countrycode)
foreach x of varlist sdds_met sdds_ctry13{
	gen lndn13_`x'=lndn13*`x'
	gen `x'_fiw=`x'*fiw
	gen lndn13_fiw_`x'=lndn13_fiw*`x'
}

//Table
local out "outreg2 using "${tables}\table_6", bracket nocons nor2 dec(3)"

*column 1: selection
reghdfe lngdp14 lndn13 lndn13_sdds_ctry13 if fiw!=., absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
*column 2: selection+treatment
reghdfe lngdp14 sdds_met lndn13 lndn13_sdds_ctry13 lndn13_sdds_met if fiw!=., absorb(countrycode year) cluster(countrycode) nocons
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 3: selection+treatment + FIW
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 sdds_met fiw fiw2 lndn13 lndn13_sdds_ctry13 lndn13_sdds_met lndn13_fiw, absorb(countrycode year) cluster(countrycode) nocons
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(T6C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 sdds_met fiw fiw2 lndn13 lndn13_sdds_ctry13 lndn13_sdds_met lndn13_fiw, absorb(countrycode year) cluster(countrycode) nocons
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 4: what if I allow the selection hetero to vary by regime
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 sdds_met fiw fiw2 sdds_ctry13_fiw lndn13 lndn13_fiw lndn13_sdds_ctry13 lndn13_fiw_sdds_ctry13 lndn13_sdds_met, absorb(countrycode year) cluster(countrycode) nocons
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
	return scalar sigma1 = `iqr'*(_b[lndn13_fiw]+_b[lndn13_fiw_sdds_ctry13])/(_b[lndn13]+_b[lndn13_sdds_ctry13])
exit
end
bootstrap r(sigma) r(sigma1), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
local sigma1: di %4.3f a[1,2]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
local sigma1SE: di %4.3f sqrt(b[2,2])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local sigma1_95_low: di %3.2f c[1,2]
local sigma1_95_high: di %3.2f c[2,2]
reghdfe lngdp14 sdds_met fiw fiw2 sdds_ctry13_fiw lndn13 lndn13_fiw lndn13_sdds_ctry13 lndn13_fiw_sdds_ctry13 lndn13_sdds_met, absorb(countrycode year) cluster(countrycode) nocons
`out' addtext(Country FE, Yes, Year FE, Yes, sigma_other,`sigma', sigma_other SE, [`sigmaSE'], sigma_other 95 CI, [`sigma_95_low' `sigma_95_high'], sigma_SDDS_ctry,`sigma1', sigma_SDDS_ctry SE, [`sigma1SE'], sigma_SDDS_ctry 95 CI, [`sigma1_95_low' `sigma1_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 5: Hetero treatment
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 sdds_met fiw fiw2 sdds_ctry13_fiw sdds_met_fiw lndn13 lndn13_fiw lndn13_sdds_ctry13 lndn13_fiw_sdds_ctry13 lndn13_sdds_met lndn13_fiw_sdds_met, absorb(countrycode year) cluster(countrycode) nocons
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
	return scalar sigma1 = `iqr'*(_b[lndn13_fiw]+_b[lndn13_fiw_sdds_ctry13])/(_b[lndn13]+_b[lndn13_sdds_ctry13])
	return scalar sigma2 = `iqr'*(_b[lndn13_fiw]+_b[lndn13_fiw_sdds_ctry13]+_b[lndn13_fiw_sdds_met])/(_b[lndn13]+_b[lndn13_sdds_ctry13]+_b[lndn13_sdds_met])
exit
end
bootstrap r(sigma) r(sigma1) r(sigma2), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
local sigma1: di %4.3f a[1,2]
local sigma2: di %4.3f a[1,3]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
local sigma1SE: di %4.3f sqrt(b[2,2])
local sigma2SE: di %4.3f sqrt(b[3,3])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local sigma1_95_low: di %3.2f c[1,2]
local sigma1_95_high: di %3.2f c[2,2]
local sigma2_95_low: di %3.2f c[1,3]
local sigma2_95_high: di %3.2f c[2,3]
reghdfe lngdp14 sdds_met fiw fiw2 sdds_ctry13_fiw sdds_met_fiw lndn13 lndn13_fiw lndn13_sdds_ctry13 lndn13_fiw_sdds_ctry13 lndn13_sdds_met lndn13_fiw_sdds_met, absorb(countrycode year) cluster(countrycode) nocons
`out' addtext(Country FE, Yes, Year FE, Yes, sigma_other,`sigma', sigma_other SE, [`sigmaSE'], sigma_other 95 CI, [`sigma_95_low' `sigma_95_high'], sigma_SDDS_ctry,`sigma1', sigma_SDDS_ctry SE, [`sigma1SE'], sigma_SDDS_ctry 95 CI, [`sigma1_95_low' `sigma1_95_high'], sigma_SDDS_post,`sigma2', sigma_SDDS_post SE, [`sigma2SE'], sigma_SDDS_post 95 CI, [`sigma2_95_low' `sigma2_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)

//Event study figure
*create leads and lags
gen sdds_lag0=sdds_met==1&sdds_met[_n-1]==0&countrycode==countrycode[_n-1]&year==year[_n-1]+1&sdds_ctry13==1
list countryname year if sdds_lag0==1 //Table A5
forvalues x=1/14{	//for the earliest adopters, lag14 is 2013, final year of the main sample
	local y=`x'-1
	gen sdds_lag`x'=sdds_lag`y'==0&sdds_lag`y'[_n-1]==1&countrycode==countrycode[_n-1]&year==year[_n-1]+1
}
gen sdds_lead1=sdds_met[_n+1]==1&sdds_met==0&countrycode==countrycode[_n+1]&year==year[_n+1]-1&sdds_ctry13==1
forvalues x=2/20{	//for the latest adopters, lead20 is 1992, first year of the main sample
	local y=`x'-1
	gen sdds_lead`x'=sdds_lead`y'==0&sdds_lead`y'[_n+1]==1&countrycode==countrycode[_n+1]&year==year[_n+1]-1
}
*aggregate endpoints
egen sdds_lag7p=rowtotal(sdds_lag7 sdds_lag8 sdds_lag9 sdds_lag10 sdds_lag11 sdds_lag12 sdds_lag13 sdds_lag14)
egen sdds_lead7p=rowtotal(sdds_lead7 sdds_lead8 sdds_lead9 sdds_lead10 sdds_lead11 sdds_lead12 sdds_lead13 sdds_lead14 sdds_lead15 sdds_lead16 sdds_lead17 sdds_lead18 sdds_lead19 sdds_lead20)
*interact with lights
foreach x of varlist sdds_lead7p sdds_lead6 sdds_lead5 sdds_lead4 sdds_lead3 sdds_lead2 sdds_lag0 sdds_lag1 sdds_lag2 sdds_lag3 sdds_lag4 sdds_lag5 sdds_lag6 sdds_lag7p{
	gen lndn13_`x'=lndn13*`x'
}
*regression
reghdfe lngdp14 lndn13_sdds_ctry13 lndn13 sdds_lead7p sdds_lead6 sdds_lead5 sdds_lead4 sdds_lead3 sdds_lead2 sdds_lag0 sdds_lag1 sdds_lag2 sdds_lag3 sdds_lag4 sdds_lag5 sdds_lag6 sdds_lag7p lndn13_sdds_lead7p-lndn13_sdds_lag7p if fiw!=., absorb(countrycode year) cluster(countrycode) nocons
parmest,saving(${temp}\sdds_event,replace)
*draw graph
preserve
	use ${dir}\Data\temp\sdds_event,clear
	keep in 18/29
	gen id=0 in 6
	replace id=id[_n-1]+1 in 7/12
	replace id=[_n]-7 in 1/5
	set obs 13
	replace id=-1 if id==.
	mvencode estimate min95 max95,mv(0)
	sort id
	twoway (scatter estimate id,mcolor(navy)) (rcap min95 max95 id) , ytitle(Change in NTL Elasticity of GDP) yline(0) ylabel(, angle(horizontal) format(%3.2f)) xtitle(Years since meeting SDDS standards) xlabel(-6(2)6) xline(0, lpattern(dash)) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(event_sdds,replace)
	graph export "${figures}\figure_A16.pdf", as(pdf) replace
restore

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//////////////////// MAIN FIGURES ////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////

//////////////////////////////////
////// Figure 1: Raw data ////////
//////////////////////////////////

use "${dir}\Data\master\Estimations", clear

preserve
replace gdp14_growth=gdp14_growth*100
replace dn13_growth=dn13_growth*100
sum dn13_growth if year>=1992&year<=2013&fiw!=.,det
binscatter gdp14_growth dn13_growth if dn13_growth>=-30&dn13_growth<=50&year>=1992&year<=2013&fiw!=.,by(autocracyFH) colors(gs10 gs5) msymbols(circle_hollow triangle) linetype(lfit) legend(r(1) order(2 "Autocracy" 1 "Democracy") pos(11) ring(0)) ytitle("GDP growth (%)") ylabel(,angle(horizontal)) xtitle("Growth of Lights Digital Number (%)") xlabel(-30(10)40) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(raw_scatter,replace) reportreg
graph export "${figures}\figure_1.eps", as(eps) replace
restore

///////////////////////////////////////////////////////
////// Figure 2: Country-specific elasticities ////////
///////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

*regression estimating country-specific GDP elasticities of NTL
reghdfe lngdp14 c.lndn13#ibn.countrycode_num if fiw!=., absorb(countrycode year) cluster(countrycode) nocons
parmest,saving(${temp}\ctry_ind,replace)
*merge with country codes/names
collapse fiw autocracyFH countrycode_num if e(sample),by(countryname countrycode)
tempfile codes
save `codes'
use ${temp}\ctry_ind,clear
gen countrycode_num=subinstr(parm,".countrycode_num#c.lndn13","",.)
destring countrycode_num,replace
merge 1:1 countrycode using `codes'	
*define FH dummies based on collapsed average
gen Dfree=(fiw<2) if fiw!=.
gen Dpfree=(fiw>=2&fiw<=4) if fiw!=.
gen Dnfree=(fiw>4) if fiw!=.

sort estimate
gen id_estimate=2*([_n]) //facilitates visualization

*average elasticity across countries by FIW
binscatter estimate fiw, line(lfit) nodraw
disp `"`e(graphcmd)'"'
twoway (scatteri .1111145055089444 0 .1797169820040415 .0868699776807002 .1404953218259154 .2398989929093255 .0489027626135612 .3446969654824998 .278630601430904 .6255411177873611 .2677519793499449 1.005772007836236 .1512715035156844 1.312215699089898 .29291502320554 1.674873736169603, mfcolor(gs12) mlcolor(gs12) msize(medlarge) msymbol(circle)) (scatteri .1186927465572996 2.068181845876905 .326263331077793 2.443181800842285 .2460888608076296 2.724747472339206 .3492705158173431 3.048863649368286 .3666006813735939 3.54134202003479 .3227041127763332 3.801593133381435, mfcolor(gs8) mlcolor(gs8) msize(medlarge) msymbol(square)) (scatteri .2418603944147924 4.229261350631714 .5577039061099932 4.585978878868951 .3897293574574408 4.772727224561903 .2650910000017882 5.143819120195177 .3908152570896083 5.855345090230306, mfcolor(gs4) mlcolor(gs4) msize(medlarge) msymbol(triangle)) (function 0*x^2+.0480636768674317*x+.1437970182165519, range(0 5.855345090230306) lcolor(gs10) lpattern(solid)), scheme(sj) graphregion(fcolor(white))  xtitle(fiw) ytitle(estimate) legend(order(1 "Free" 2 "Partially Free" 3 "Not Free") r(1) pos(11) ring(0)) ylabel(,angle(horizontal)) ytitle(NTL Elasticity of GDP) xtitle(Freedom in the World Index) name(scatter_ctry,replace)
graph export "${figures}\figure_2.eps", as(eps) replace

*plot of all the country-level elasticities
twoway (scatter estimate id_estimate if Dfree==1, mfcolor(green%30) mlcolor(black) msize(medium) msymbol(circle) jitter(10) jitterseed(1110110)) (scatter estimate id_estimate if Dpfree==1, mfcolor(yellow%60) mlcolor(black) msize(medium) msymbol(triangle) jitter(10) jitterseed(1110110)) (scatter estimate id_estimate if Dnfree==1, mfcolor(red) mlcolor(black) msize(medium) msymbol(square) jitter(10) jitterseed(1110110)), ytitle(NTL Elasticity of GDP) ylabel(,angle(horizontal)) xlabel(0 "0" 100 "50" 200 "100" 300 "150" 400 "200") xtitle(Country) legend(order(1 "Free (N=87)" 2 "Partially Free (N=53)" 3 "Not Free (N=44)") rows(1)) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(ctry_ind,replace)
graph export "${figures}\figure_A5.pdf", as(pdf) replace

////////////////////////////////
////// Figure 3: Sigmas ////////
////////////////////////////////

*Figure is produced at the end of this do-file using information from all other estimations

///////////////////////////////////////
////// Figure 4: Event studies ////////
///////////////////////////////////////

//into dictatorship

use "${dir}\Data\master\Estimations", clear

sort countrycode year
*create transition events and remove unbalanced spells (adjusting based on total leads and lags can be misleading because some countries have multiple events) 
gen aut_in_lag0=autocracyFH==1&autocracyFH[_n-1]==0&countrycode==countrycode[_n-1]&year>=1992&year<=2013
replace aut_in_lag0=0 if aut_in_lag0==1&(countrycode=="BGD"|countrycode=="DOM"|countrycode=="MDV"|countrycode=="MLI"|countrycode=="NIC"|countrycode=="PHL"|countrycode=="SLE") //<4 lags
replace aut_in_lag0=0 if aut_in_lag0==1&(countrycode=="DJI"|countrycode=="GMB"|countrycode=="MRT") //<4 leads
replace aut_in_lag0=0 if aut_in_lag0==1&(countrycode=="BIH"|(countrycode=="CAF"&year==2008)|countrycode=="FJI"|countrycode=="PER"|(countrycode=="GEO"&year==2003)|(countrycode=="GNB"&year==2003)|countrycode=="NER")	//short events in multi-event countries
list countryname year if aut_in_lag0==1 //list of countries for Table A3
*create leads and lags
forvalues x=1/25{
	local y=`x'-1
	gen aut_in_lag`x'=aut_in_lag`y'==0&aut_in_lag`y'[_n-1]==1&countrycode==countrycode[_n-1]&year==year[_n-1]+1&year>=1992&year<=2013
	replace aut_in_lag`x'=0 if autocracyFH==0
}
gen aut_in_lead1=aut_in_lag0[_n+1]==1&aut_in_lag0==0&countrycode==countrycode[_n+1]&year==year[_n+1]-1&year>=1992&year<=2013
forvalues x=2/25{
	local y=`x'-1
	gen aut_in_lead`x'=aut_in_lead`y'==0&aut_in_lead`y'[_n+1]==1&countrycode==countrycode[_n+1]&year==year[_n+1]-1&year>=1992&year<=2013
	replace aut_in_lead`x'=0 if autocracyFH==1
}
*create endpoint dummies at -7 or less and +7 or more
egen aut_in_lag7p=rowtotal(aut_in_lag7 - aut_in_lag25)
egen aut_in_lead7p=rowtotal(aut_in_lead7 - aut_in_lead25)
*interact leads and lags with lights
foreach x of varlist aut_in_lead7p aut_in_lead6 aut_in_lead5 aut_in_lead4 aut_in_lead3 aut_in_lead2 aut_in_lead1 aut_in_lag0 aut_in_lag1 aut_in_lag2 aut_in_lag3 aut_in_lag4 aut_in_lag5 aut_in_lag6 aut_in_lag7p {
	gen lndn13_`x'=lndn13*`x'
}
*I create a dummy for autocracy observations not in these spells
egen temp=rowtotal(aut_in_lag0 aut_in_lag1 aut_in_lag2 aut_in_lag3 aut_in_lag4 aut_in_lag5 aut_in_lag6 aut_in_lag7p),miss
gen autocracyFH_other=autocracyFH-temp
gen lndn13_autocracyFH_other=lndn13*autocracyFH_other
drop temp
*regression
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 aut_in_lead7p aut_in_lead6 aut_in_lead5 aut_in_lead4 aut_in_lead3 aut_in_lead2 aut_in_lead1 aut_in_lag0 aut_in_lag1 aut_in_lag2 aut_in_lag3 aut_in_lag4 aut_in_lag5 aut_in_lag6 aut_in_lag7p autocracyFH_other lndn13 lndn13_autocracyFH_other lndn13_aut_in_lead7p-lndn13_aut_in_lag7p if fiw!=., absorb(countrycode year) cluster(countrycode)
	return scalar sigma = _b[lndn13_autocracyFH_other]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
reghdfe lngdp14 aut_in_lead7p aut_in_lead6 aut_in_lead5 aut_in_lead4 aut_in_lead3 aut_in_lead2 aut_in_lead1 aut_in_lag0 aut_in_lag1 aut_in_lag2 aut_in_lag3 aut_in_lag4 aut_in_lag5 aut_in_lag6 aut_in_lag7p autocracyFH_other lndn13 lndn13_autocracyFH_other lndn13_aut_in_lead7p-lndn13_aut_in_lag7p if fiw!=., absorb(countrycode year) cluster(countrycode)
parmest,saving(${temp}\autoc_in_event,replace)
*draw graph
preserve
use ${temp}\autoc_in_event,clear
keep in 20/32
gen id=0 in 7
replace id=id[_n-1]+1 in 8/13
replace id=[_n]-7 in 1/6
sort id
twoway (scatter estimate id,mcolor(gs5) msymbol(triangle)) (rcap min95 max95 id), ytitle(Change in NTL elasticity of GDP) yline(0) ylabel(-.1(0.1)0.3, angle(horizontal) format(%3.2f)) xtitle(Years since becoming Autocracy) xlabel(-6(2)6) xline(0, lpattern(dash)) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) note(Other autocracy {&sigma}: `sigma' (`sigmaSE')) name(event_autoc_in,replace)
graph export "${figures}\figure_4a.eps", as(eps) replace
restore

//out of dictatorship

use "${dir}\Data\master\Estimations", clear

sort countrycode year
*create transition events and remove unbalanced spells
gen aut_out_lag0=autocracyFH==0&autocracyFH[_n-1]==1&countrycode==countrycode[_n-1]&year>=1992&year<=2013
replace aut_out_lag0=0 if aut_out_lag0==1&(countrycode=="BTN"|countrycode=="DJI"|countrycode=="GMB"|countrycode=="HND"|countrycode=="LBY"|countrycode=="MDV"|countrycode=="MRT"|countrycode=="NPL"|countrycode=="PAK"|countrycode=="TUN") //<4 lags
replace aut_out_lag0=0 if aut_out_lag0==1&(countrycode=="BGD"|countrycode=="DOM"|countrycode=="FJI"|countrycode=="KGZ"|countrycode=="MDA"|countrycode=="MDG"|countrycode=="MOZ"|countrycode=="MWI"|countrycode=="PHL"|countrycode=="PRY"|countrycode=="RUS"|countrycode=="SYC"|countrycode=="UKR"|countrycode=="ZAF") //<4 leads
replace aut_out_lag0=0 if aut_out_lag0==1&(countrycode=="ALB"|countrycode=="CAF"|countrycode=="COG"|countrycode=="GEO"|countrycode=="GNB"|countrycode=="GUY"|countrycode=="MLI"|countrycode=="NER"|countrycode=="ROU"|countrycode=="SLE"|countrycode=="THA"|countrycode=="TLS"|(countrycode=="BIH"&year==1996)|(countrycode=="HTI"&year==1994)|(countrycode=="KEN"&year==2013)|(countrycode=="LSO"&year==1993))	// short events in multi-event countries
list countryname year if aut_out_lag0==1 //list of countries for Table C3
*create leads and lags
forvalues x=1/25{
	local y=`x'-1
	gen aut_out_lag`x'=aut_out_lag`y'==0&aut_out_lag`y'[_n-1]==1&countrycode==countrycode[_n-1]&year==year[_n-1]+1&year>=1992&year<=2013
	replace aut_out_lag`x'=0 if autocracyFH==1
}
gen aut_out_lead1=aut_out_lag0[_n+1]==1&aut_out_lag0==0&countrycode==countrycode[_n+1]&year==year[_n+1]-1&year>=1992&year<=2013
forvalues x=2/25{
	local y=`x'-1
	gen aut_out_lead`x'=aut_out_lead`y'==0&aut_out_lead`y'[_n+1]==1&countrycode==countrycode[_n+1]&year==year[_n+1]-1&year>=1992&year<=2013
	replace aut_out_lead`x'=0 if autocracyFH==0
}
*create endpoint dummies at -7 or less and +7 or more
egen aut_out_lag7p=rowtotal(aut_out_lag7 - aut_out_lag25)
egen aut_out_lead7p=rowtotal(aut_out_lead7 - aut_out_lead25)
*interact leads and lags with lights
foreach x of varlist aut_out_lead7p aut_out_lead6 aut_out_lead5 aut_out_lead4 aut_out_lead3 aut_out_lead2 aut_out_lead1 aut_out_lag0 aut_out_lag1 aut_out_lag2 aut_out_lag3 aut_out_lag4 aut_out_lag5 aut_out_lag6 aut_out_lag7p{
	gen lndn13_`x'=lndn13*`x'
}
*I create a dummy for autocracy observations not in these spells
egen temp=rowtotal(aut_out_lead1 aut_out_lead2 aut_out_lead3 aut_out_lead4 aut_out_lead5 aut_out_lead6 aut_out_lead7p),miss
gen autocracyFH_other=autocracyFH-temp
gen lndn13_autocracyFH_other=lndn13*autocracyFH_other
drop temp
*regression
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 aut_out_lead7p aut_out_lead6 aut_out_lead5 aut_out_lead4 aut_out_lead3 aut_out_lead2 aut_out_lead1 aut_out_lag0 aut_out_lag1 aut_out_lag2 aut_out_lag3 aut_out_lag4 aut_out_lag5 aut_out_lag6 aut_out_lag7p autocracyFH_other lndn13 lndn13_autocracyFH_other lndn13_aut_out_lead7p-lndn13_aut_out_lag7p if fiw!=., absorb(countrycode year) cluster(countrycode)
	return scalar sigma = _b[lndn13_autocracyFH_other]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
reghdfe lngdp14 aut_out_lead7p aut_out_lead6 aut_out_lead5 aut_out_lead4 aut_out_lead3 aut_out_lead2 aut_out_lead1 aut_out_lag0 aut_out_lag1 aut_out_lag2 aut_out_lag3 aut_out_lag4 aut_out_lag5 aut_out_lag6 aut_out_lag7p autocracyFH_other lndn13 lndn13_autocracyFH_other lndn13_aut_out_lead7p-lndn13_aut_out_lag7p if fiw!=., absorb(countrycode year) cluster(countrycode)
parmest,saving(${temp}\autoc_out_event,replace)
*draw graph
preserve
use ${temp}\autoc_out_event,clear
keep in 20/32
gen id=0 in 7
replace id=id[_n-1]+1 in 8/13
replace id=[_n]-7 in 1/6
sort id
twoway (scatter estimate id,mcolor(gs10)) (rcap min95 max95 id), ytitle(Change in NTL elasticity of GDP) yline(0) ylabel(, angle(horizontal) format(%3.2f)) xtitle(Years since becoming Democracy) xlabel(-6(2)6) xline(0, lpattern(dash)) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) note(Other autocracy {&sigma}: `sigma' (`sigmaSE')) name(event_autoc_out,replace)
graph export "${figures}\figure_4b.eps", as(eps) replace
restore

/////////////////////////////////////////
////// Figure 5: IDA event study ////////
/////////////////////////////////////////

*Figure is produced with Table 4 above

///////////////////////////////////////////////////////
////// Figures 6 and A14-A15: Adjustment of GDP ////////
///////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

*creation of variables
foreach x in lngdp14 lndn13 lncons13{
	gen temp=`x'_lr if year==1992
	egen temp1=mean(temp),by(countrycode)
	gen temp2=`x'_lr if year==2013
	egen temp3=mean(temp2),by(countrycode)
	gen `x'_diff_9213=temp3-temp1
	drop temp*
}

keep if year==2013
gsort -lngdp14_diff_9213 //sort by raw GDP long diff
drop if fiw_avg9213==.|lngdp14_diff_9213==.	//get rid of countries with no FIW
drop if countrycode=="SGP"|countrycode=="GNQ"|countrycode=="BHR"
//create naive ranking based on raw data
gen ranking_naive=_n 
//adjust GDP to correct for bias (based on T1C7)
gen phi=0.032/0.288
gen bias= 1+(fiw_avg9213*phi)
gen lngdp_adj= lngdp14_diff_9213/bias 
gsort - lngdp_adj
//adjusted ranking
gen ranking_adj=_n 
gen switch=ranking_naive-ranking_adj
xtile ptile_naive = ranking_naive,nq(100)
replace ptile_naive=100-ptile_naive
xtile ptile_adj = ranking_adj,nq(100)
replace ptile_adj=100-ptile_adj
//ranking based on change in lights 92-13
gsort -lndn13_diff_9213
gen ranking_adj_dn=_n 
gen switch_dn=ranking_naive-ranking_adj_dn
xtile ptile_adj_dn = ranking_adj_dn,nq(100)
replace ptile_adj_dn=100-ptile_adj_dn
//long-run difference in private consumption
gsort -lncons13_diff_9213 
gen ranking_adj_cons=_n
xtile ptile_adj_cons = ranking_adj_cons,nq(100)
replace ptile_adj_cons=100-ptile_adj_cons

twoway (function y=x,range(1 100) lpattern(solid) lwidth(vthin) lcolor(gs13)) (scatter ptile_adj ptile_naive if lngdp_adj<., msymbol(none) mlabel(iso3v10) mlabsize(tiny) mlabposition(0)), ytitle({bf:Adjusted} GDP growth (1992-2013): Percentile) ylabel(1 20 40 60 80 100, angle(horizontal) nogrid) xtitle({bf:Raw} GDP growth (1992-2013): Percentile) xlabel(1 20 40 60 80 100) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(scatter_adj,replace)
graph export "${figures}\figure_A15.pdf", as(pdf) replace

sort ranking_naive
sum fiw_avg9213 if _n<=20
twoway (bar lngdp14_diff_9213 ranking_naive if _n<=20&Dfree_avg9213==1, fcolor(white)) (bar lngdp14_diff_9213 ranking_naive if _n<=20&Dpfree_avg9213==1, fcolor(gs8)) (bar lngdp14_diff_9213 ranking_naive if _n<=20&Dnfree_avg9213==1, fcolor(gs0)), ytitle("Change in log GDP: 1992-2013") ylabel(0.8(0.2)2, angle(horizontal)) xtitle("") xlabel(1 "Liberia" 2 "China" 3 "Cabo Verde" 4 "Angola" 5 "Cambodia" 6 "Ethiopia" 7 "Bhutan" 8 "Mozambique" 9 "Laos" 10 "Uganda" 11 "Vietnam" 12 "India" 13 "Azerbaijan" 14 "Chad" 15 "Armenia" 16 "Mongolia" 17 "Burkina Faso" 18 "Nigeria" 19 "Ghana" 20 "Panama", labsize(small) angle(forty_five)) legend(order(1 "Free" 2 "Partially Free" 3 "Not Free") rows(1)) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(long_naive,replace)
graph export "${figures}\figure_6a.eps", as(eps) replace

sort ranking_adj
sum fiw_avg9213 if _n<=20
twoway (bar lngdp_adj ranking_adj if _n<=20&Dfree_avg9213==1, fcolor(white)) (bar lngdp_adj ranking_adj if _n<=20&Dpfree_avg9213==1, fcolor(gs8)) (bar lngdp_adj ranking_adj if _n<=20&Dnfree_avg9213==1, fcolor(gs0)), ytitle("Change in Adjusted log GDP: 1992-2013") ylabel(0.8(0.2)2, angle(horizontal)) xtitle("") xlabel(1 "Cabo Verde" 2 "Liberia" 3 "China" 4 "Mozambique" 5 "India" 6 "Mongolia" 7 "Panama" 8 "Ethiopia" 9 "Ghana" 10 "Angola" 11 "Cambodia" 12 "Uganda" 13 "Bhutan" 14 "Dom. Republic" 15 "Ireland" 16 "Armenia" 17 "South Korea" 18 "Costa Rica" 19 "Azerbaijan" 20 "Chile", labsize(small) angle(forty_five)) legend(order(1 "Free" 2 "Partially Free" 3 "Not Free") rows(1)) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(long_adj,replace)
graph export "${figures}\figure_6b.eps", as(eps) replace

twoway (function y=x,range(1 100) lpattern(dash) lwidth(vthin) lcolor(gs5)) (lfit ptile_adj_dn ptile_naive ,lpattern(solid) lwidth(vthin) lcolor(gs13)) (scatter ptile_adj_dn ptile_naive if lngdp_adj<., msymbol(none) mlabel(iso3v10) mlabsize(tiny) mlabposition(0)), ytitle(NTL Growth (1992-2013): Percentile) ylabel(1 20 40 60 80 100, angle(horizontal) nogrid) xtitle({bf:Raw} GDP growth (1992-2013): Percentile) xlabel(1 20 40 60 80 100) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(scatter_adj_dn,replace)
graph export "${figures}\figure_A14.pdf", as(pdf) replace

/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
//////////////////// APPENDIX TABLES ////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////
////// Table A1: Political chars from IAEP ////////
///////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

//creation of variables

//dummy equal to one in years after new constitution
gen temp=constage==1 if constage<.&year>=1992
bysort countrycode (year): gen new_const=sum(temp) if year<=2012&constage<.
replace new_const=1 if new_const>1&new_const<.
drop temp
//dummy for unicameral legislature
gen legcham1=(legcham==1) if legcham<. 
//binary measure of federalism
gen federal=inlist(govstruct,2,3) if govstruct<.
//binary measure of autonomous regional gvt
gen region_auto=regstruct==1 if regstruct<.

preserve
collapse fiw_avg9213 (max) new_const (mean) leg legcham1 elecleg elecexec suffreg banned banall stateparty execveto legveto removeleg removeexec execforce exectax execbudget court federal region_auto  if year>=1992&year<=2012, by(countrycode)

local out "outreg2 using "${tables}\table_A1a", sideway bracket nocons" 

local replace replace
foreach x of varlist leg legcham1 elecleg elecexec suffreg banned banall stateparty execveto legveto removeleg removeexec execforce exectax execbudget new_const court federal region_auto{
	reg fiw_avg9213 `x', vce(robust)
	sum `x' if e(sample)
	local mean: di %3.2f r(mean)
	if "`x'"=="region_auto" local replace tex(frag)
	`out' addstat(Mean ,`mean') `replace'	
	local replace
}
restore

local out "outreg2 using "${tables}\table_A1b", sideway bracket nocons nor2" 

local replace replace
foreach x of varlist leg legcham1 elecleg elecexec suffreg banned banall stateparty execveto legveto removeleg removeexec execforce exectax execbudget new_const court federal region_auto{
	reghdfe fiw `x' if lndn13!=.&lngdp14!=., absorb(countrycode year) cluster(countrycode)
	sum `x' if e(sample)
	local mean: di %3.2f r(mean)
	if "`x'"=="region_auto" local replace tex(frag)
	`out' addtext(Country FE, x, Year FE, x) addstat(Countries, e(N_clust), R-squared, e(r2_within), Mean ,`mean') `replace'
	local replace
}

/////////////////////////////////////////////////////////////////////////
////// Table A2: Comparison of means across average FIW categories////////
/////////////////////////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

local if if fiw!=.&dn13_growth!=.&gdp14_growth!=.

*average GDP growth: total and FIW categories
mean gdp14_growth `if'
mean gdp14_growth `if'&Dfree==1
mean gdp14_growth `if'&Dpfree==1
mean gdp14_growth `if'&Dnfree==1

*average DN growth: total and FIW categories
mean dn13_growth `if'
mean dn13_growth `if'&Dfree==1
mean dn13_growth `if'&Dpfree==1
mean dn13_growth `if'&Dnfree==1

*I create a temporary variable with the FIW categories to do the t-tests 
gen testvar=1 if Dfree==1
replace testvar=2 if Dpfree==1
replace testvar=3 if Dnfree==1
ttest gdp14_growth `if'&testvar!=1, by(testvar) unequal
ttest gdp14_growth `if'&testvar!=2, by(testvar) unequal
ttest gdp14_growth `if'&testvar!=3, by(testvar) unequal
ttest dn13_growth `if'&testvar!=1, by(testvar) unequal
ttest dn13_growth `if'&testvar!=2, by(testvar) unequal
ttest dn13_growth `if'&testvar!=3, by(testvar) unequal
drop testvar

////////////////////////////////////////////
////// Table D1: Composition of GDP ////////
////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

local out "outreg2 using "${tables}\table_D1", bracket nocons nor2 dec(3) keep(lndn13 fiw fiw2 lndn13_fiw x lndn13_x)" 
local cond_D1 cons_gdp!=.&inv_gdp!=.&gov_gdp!=.&exp_gdp!=.&imp_gdp!=.

*column 1: reduced sample
gen x=.
gen lndn13_x=.
capture program drop sigma_boot
program define sigma_boot, rclass
	args cond
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if `cond', absorb(countrycode year) cluster(countrycode) 
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot `cond_D1'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD1C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if `cond_D1', absorb(countrycode year) cluster(countrycode) 
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(baseline) replace
local i=`i'+1
drop x lndn13_x
*column 2: consumption
gen x=cons_gdp
gen lndn13_x=lndn13*x
capture program drop sigma_boot_x
program define sigma_boot_x, rclass
	args cond
	reghdfe lngdp14 lndn13 fiw fiw2 x lndn13_fiw lndn13_x if `cond', absorb(countrycode year) cluster(countrycode) 
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x `cond_D1'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD1C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 x lndn13_fiw lndn13_x if `cond_D1', absorb(countrycode year) cluster(countrycode) 
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(cons)
local i=`i'+1
drop x lndn13_x
*column 3: investment
gen x=inv_gdp
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x `cond_D1'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD1C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 x lndn13_fiw lndn13_x if `cond_D1', absorb(countrycode year) cluster(countrycode) 
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(inv)
local i=`i'+1
drop x lndn13_x
*column 4: gvt spending
gen x=gov_gdp
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x `cond_D1'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD1C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 x lndn13_fiw lndn13_x if `cond_D1', absorb(countrycode year) cluster(countrycode) 
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(gov)
local i=`i'+1
drop x lndn13_x
*column 5: exports
gen x=exp_gdp
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x `cond_D1'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD1C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 x lndn13_fiw lndn13_x if `cond_D1', absorb(countrycode year) cluster(countrycode) 
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(exp)
local i=`i'+1
drop x lndn13_x
*column 6: imports
gen x=imp_gdp
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x `cond_D1'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD1C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 x lndn13_fiw lndn13_x if `cond_D1', absorb(countrycode year) cluster(countrycode) 
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(imp)
local i=`i'+1
*column 7: all
capture program drop sigma_boot
program define sigma_boot, rclass
	args cond
	reghdfe lngdp14 lndn13 fiw fiw2 cons_gdp inv_gdp gov_gdp exp_gdp lndn13_fiw c.lndn13#c.cons_gdp c.lndn13#c.inv_gdp c.lndn13#c.gov_gdp c.lndn13#c.exp_gdp if `cond', absorb(countrycode year) cluster(countrycode) 
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot `cond_D1'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD1C7) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 cons_gdp inv_gdp gov_gdp exp_gdp lndn13_fiw c.lndn13#c.cons_gdp c.lndn13#c.inv_gdp c.lndn13#c.gov_gdp c.lndn13#c.exp_gdp if `cond_D1', absorb(countrycode year) cluster(countrycode) 
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(all) tex(frag)
local i=`i'+1
drop x lndn13_x

////////////////////////////////////////////
////// Table D2: Sectoral Composition /////
////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

local out "outreg2 using "${tables}\table_D2", bracket nocons nor2 dec(3)" 

capture program drop sigma_boot_x
program define sigma_boot_x, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end

*column 1: agriculture (%land)
gen x=ag_lnd_agri_zs_2014_11
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD2C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(agr_land) replace
local i=`i'+1
drop x lndn13_x
*column 2: agriculture (% GDP)
gen x=nv_agr_totl_zs_2014_11
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD2C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(agr_gdp) 
local i=`i'+1
drop x lndn13_x
*column 3: natural resources (% GDP)
gen x=ny_gdp_totl_rt_zs_2014_11
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD2C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(natres_gdp) 
local i=`i'+1
drop x lndn13_x
*column 4: oil rents (% GDP)
gen x=ny_gdp_petr_rt_zs_2014_11
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD2C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(oil_gdp) 
local i=`i'+1
drop x lndn13_x
*column 5: industry (% GDP)
gen x=nv_ind_totl_zs_2014_11
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD2C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(ind_gdp) 
local i=`i'+1
drop x lndn13_x
*column 6: manufacturing (% GDP)
gen x=nv_ind_manf_zs_2014_11
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD2C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(man_gdp) 
local i=`i'+1
drop x lndn13_x
*column 7: services (% GDP)
gen x=nv_srv_tetc_zs_2014_11
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD2C7) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(svc_gdp) tex(frag)
local i=`i'+1
drop x lndn13_x

//////////////////////////////////////
////// Table D3: Chars of NTL ////////
//////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

//create vars
*polynomial of lights
forvalues x =2/4{
	gen lndn13_`x'=lndn13^`x'
}

*interactions of lights with latitude and longitude
foreach x in lat long{
	gen lndn13_`x'=lndn13*`x'itude
	gen lndn13_`x'2=lndn13*`x'itude*`x'itude
}

local out "outreg2 using "${tables}\table_D3", bracket nocons nor2 dec(3) drop(lndn13_2 lndn13_3 lndn13_4 lndn13_long lndn13_long2 lndn13_lat lndn13_lat2)"

*column 1: polynomial in lights

capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 lndn13_2 lndn13_3 lndn13_4 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end

bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD3C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 lndn13_2 lndn13_3 lndn13_4 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
local i=`i'+1
*column 2: year-specific lights coefficient 
reghdfe lngdp14 fiw fiw2 lndn13_fiw, absorb(countrycode year c.lndn13#i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) 
*column 3: quadratic in lat/long x lights

capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 lndn13_long lndn13_long2 lndn13_lat lndn13_lat2 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13] //lat long (0,0) is Null island off West Africa
exit
end

bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD3C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 lndn13_long lndn13_long2 lndn13_lat lndn13_lat2 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 4: subregion-specific lights elasticity
reghdfe lngdp14 fiw fiw2 lndn13_fiw, absorb(countrycode year c.lndn13#i.subsubregcode_un) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) 
*column 5: lights hetero by area
gen x=ag_lnd_totl_k2/100000
gen lndn13_x=lndn13*x

capture program drop sigma_boot_x
program define sigma_boot_x, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_x lndn13_fiw, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end

bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD3C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_x lndn13_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
drop x lndn13_x
*column 6: lights hetero by share top-coded

capture program drop sigma_boot_x //must define again because area is time-invariant
program define sigma_boot_x, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 x lndn13_x lndn13_fiw, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end

gen x=count_topcoded/count_lights
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD3C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 x lndn13_x lndn13_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
drop x lndn13_x
*column 7: lights hetero by share unlit
gen x=count_unlit/count_lights
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD3C7) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 x lndn13_x lndn13_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
drop x lndn13_x
*column 8: lights hetero by Gini
gen x=lgini13
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD3C8) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 x lndn13_x lndn13_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)
local i=`i'+1
drop x lndn13_x

///////////////////////////////////////////////////////////////////
////// Table D4: Excluding highest shares unlit and top-coded /////
///////////////////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

*create variables
gen sh_unlit= count_unlit/count_lights
egen avg_sh_unlit=mean(sh_unlit),by(countrycode)
gen sh_topcoded= count_topcoded/count_lights
egen avg_sh_topcoded=mean(sh_topcoded),by(countrycode)

*find percentiles (imputed by hand below)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
sum avg_sh_unlit avg_sh_topcoded if e(sample),det

local out "outreg2 using "${tables}\table_D4", bracket nocons nor2 dec(3)" 

//Program for bootstrap
capture program drop sigma_boot
program define sigma_boot, rclass
	args excl
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if `excl', absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
*column 1: dropping top 1% unlit
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot avg_sh_unlit<0.9990983
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD4C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if avg_sh_unlit<0.9990983, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
local i=`i'+1
*column 2: dropping top 5% unlit
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot avg_sh_unlit<0.9962577
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD4C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if avg_sh_unlit<0.9962577, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 3: dropping top 10% unlit
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot avg_sh_unlit<0.9926472
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD4C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if avg_sh_unlit<0.9926472, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 4: dropping top 1% top-coded
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot avg_sh_topcoded<.0510991
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD4C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if avg_sh_topcoded<.0510991, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 5: dropping top 5% top-coded
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot avg_sh_topcoded<.0170629
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD4C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if avg_sh_topcoded<.0170629, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 6: dropping top 10% top-coded
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot avg_sh_topcoded<.0061486
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD4C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if avg_sh_topcoded<.0061486, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)
local i=`i'+1

//////////////////////////////////////////////////////////////
////// Table D5: Urbanization and access to electricity //////
//////////////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

local out "outreg2 using "${tables}\table_D5", bracket nocons nor2 dec(3)" 

capture program drop sigma_boot_x
program define sigma_boot_x, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end

*column 1: population
gen x=ln(sp_pop_totl_2014_11)
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD5C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(logPop) replace
local i=`i'+1
drop x lndn13_x
*column 2: urban share
gen x=sp_urb_totl_in_zs_2014_11
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD5C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(pct_urb)
local i=`i'+1
drop x lndn13_x
*column 3: access to electricity, total
gen x=eg_elc_accs_zs
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD5C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(elec_tot)
local i=`i'+1
drop x lndn13_x
*column 4: access to electricity, urban
gen x=eg_elc_accs_ur_zs
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD5C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(elec_urb)
local i=`i'+1
drop x lndn13_x
*column 5: access to electricity, rural
gen x=eg_elc_accs_ru_zs
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD5C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(elec_rur)
local i=`i'+1
drop x lndn13_x
*column 6: total electricity consumption
gen x=ln(eg_use_elec_kh_2014_11)
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD5C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(logKWH) tex(frag)
local i=`i'+1
drop x lndn13_x

////////////////////////////////////////////////////
////// Table D6: Initial level of development /////
////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear
 
//create variables
 
bysort countrycode (year): gen whenfirst = sum(!missing(avdn13)) == 1 //area-weighted average lights digital number per cell
gen temp=avdn13 if whenfirst==1	//I do it this way to identify the first obsrvation with lights
egen first_avdn13=mean(temp),by(countrycode)
drop temp* whenfirst
gen first_lndn13=ln(first_avdn13+1)

bysort countrycode (year): gen first = sum(!missing(ny_gdp_pcap_kd_2014_11)) == 1  if year>=1992 //GDP per capita in constant USD
gen temp=ny_gdp_pcap_kd_2014_11 if first==1	//I do it this way to identify the first observation with GDP 
egen first_gdp_pc=mean(temp),by(countrycode)
drop temp*
gen first_lngdp=ln(first_gdp_pc)

foreach x of varlist first_gdp_pc first_avdn13{
	gen lndn13_`x'=lndn13*`x'
	xtile temp=`x' if first==1&lndn13!=.&fiw!=., n(5)
	tab temp
	egen `x'_q=mean(temp),by(countrycode)
	drop temp
	forvalues y=2/5{
		gen lndn13_`x'_q`y'=cond(`x'_q==`y',lndn13,0)
	}
}

gen Ddeveloping=(DevelopedDevelopingCountries=="Developing"& LeastDevelopedCountriesLDC!="x" )
gen Dleastdeveloped=(LeastDevelopedCountriesLDC =="x")
encode incomelevel, gen(inclev)
gen lndn13_Ddeveloping=lndn13*Ddeveloping
gen lndn13_Dleastdeveloped=lndn13*Dleastdeveloped

//bootstrap program with controls
capture program drop sigma_boot_x
program define sigma_boot_x, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end

//bootstrap program for quintiles
capture program drop sigma_boot_q
program define sigma_boot_q, rclass
	args cond
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x2 lndn13_x3 lndn13_x4 lndn13_x5 if `cond',absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+_b[lndn13_x3])
exit
end

local out "outreg2 using "${tables}\table_D6", bracket nocons nor2 dec(3)" 

*column 1: gdp PC
gen x=first_gdp_pc
gen lndn13_x=lndn13*x 
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD6C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(gdp_pc) replace
local i=`i'+1
drop x lndn13_x
*column 2: log GDP pc
gen x=first_lngdp
gen lndn13_x=lndn13*x 
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD6C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(Log_gdp_pc) 
local i=`i'+1
drop x lndn13_x
*column 3: Quintiles of GDP PC
gen lndn13_x2=lndn13_first_gdp_pc_q2
gen lndn13_x3=lndn13_first_gdp_pc_q3
gen lndn13_x4=lndn13_first_gdp_pc_q4
gen lndn13_x5=lndn13_first_gdp_pc_q5
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_q lndn13*first_lngdp!=.
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD6C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x2 lndn13_x3 lndn13_x4 lndn13_x5 if lndn13*first_lngdp!=.,absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(Q_gdp_pc) 
local i=`i'+1
drop lndn13_x*
*column 4: avdn
gen x=first_avdn13
gen lndn13_x=lndn13*x 
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD6C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(avdn)
local i=`i'+1
drop x lndn13_x
*column 5: lndn
gen x=first_lndn13
gen lndn13_x=lndn13*x 
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD6C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(lndn)
local i=`i'+1
drop x lndn13_x
*column 6: Quintiles of avdn
gen lndn13_x2=lndn13_first_avdn13_q2
gen lndn13_x3=lndn13_first_avdn13_q3
gen lndn13_x4=lndn13_first_avdn13_q4
gen lndn13_x5=lndn13_first_avdn13_q5
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_q lndn13*first_lndn!=.
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD6C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x2 lndn13_x3 lndn13_x4 lndn13_x5 if lndn13*first_lndn!=.,absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(Q_avdn) 
local i=`i'+1
drop lndn13_x*
*column 7:UN Development dummies
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_Ddeveloping lndn13_Dleastdeveloped, absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD6C7) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_Ddeveloping lndn13_Dleastdeveloped, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(UN_cat)
local i=`i'+1
*column 8: WB Development levels
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw c.lndn13#i.inclev, absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD6C8) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw c.lndn13#i.inclev, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(WB_cat) tex(frag)
local i=`i'+1

/////////////////////////////////////////////////////
////// Table D7: Human capital and informality //////
/////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

local out "outreg2 using "${tables}\table_D7", bracket nocons nor2 dec(3)" 

//Program for bootstrap
capture program drop sigma_boot_x
program define sigma_boot_x, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end

*column 1: mean years of education
gen x=mean_yrs_educ
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD7C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(yrs_educ) replace
local i=`i'+1
drop x lndn13_x
*column 2: net primary enrollment rate
gen x=se_prm_nenr_2014_11
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD7C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(net_prim_enr) 
local i=`i'+1
drop x lndn13_x
*column 3: life expectancy at birth
gen x=sp_dyn_le00_in_2014_11
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD7C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(life_expec) 
local i=`i'+1
drop x lndn13_x
*column 4: infant mortality rate
gen x=sp_dyn_imrt_in_2014_11 
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD7C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(inf_mort) 
local i=`i'+1
drop x lndn13_x

//redefine program for time-invariant control
capture program drop sigma_boot_x
program define sigma_boot_x, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end

*column 5: firms starting formal
gen x=start_formal 
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD7C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(start_formal) 
local i=`i'+1
drop x lndn13_x
*column 6: years before formalizing
gen x=years_informal 
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD7C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(yrs_informal) 
local i=`i'+1
drop x lndn13_x
*column 7: competition from informals
gen x=competition_informal
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD7C7) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(comp_informal) 
local i=`i'+1
drop x lndn13_x
*column 8: constrained by informals
gen x=constraint_informal
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD7C8) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(constr_informal) tex(frag)
local i=`i'+1
drop x lndn13_x

/////////////////////////////////////////////////////
////// Table D8: Statistical capacity (Aggr.) ///////
/////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

foreach x of varlist sci_iq_sci_srce sci_iq_sci_prdc sci_iq_sci_mthd sci_iq_sci_ovrl{
    replace `x'=`x'/100
}

//bootstrap program for cols 2-5
capture program drop sigma_boot_x
program define sigma_boot_x, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end

local out "outreg2 using "${tables}\table_D8", bracket nocons nor2 dec(3)" 

*column 1: baseline with reduced sample
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if sci_iq_sci_ovrl!=., absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD8C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if sci_iq_sci_ovrl!=., absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(baseline) replace 
local i=`i'+1
*column 2: sources
gen x=sci_iq_sci_srce
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD8C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(sources) 
local i=`i'+1
drop x lndn13_x
*column 3: periodicity
gen x=sci_iq_sci_prdc
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD8C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(periodicity) 
local i=`i'+1
drop x lndn13_x
*column 4: methodology
gen x=sci_iq_sci_mthd
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD8C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(methodology) 
local i=`i'+1
drop x lndn13_x
*column 5: overall
gen x=sci_iq_sci_ovrl 
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD8C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(overall) tex(frag) 
local i=`i'+1
drop x lndn13_x

///////////////////////////////////////
////// Table D9: State capacity ///////
///////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

//programs for bootstrap
capture program drop sigma_boot_if
program define sigma_boot_if, rclass
	args cond
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if `cond', absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end

capture program drop sigma_boot_x
program define sigma_boot_x, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end

local out "outreg2 using "${tables}\table_D9", bracket nocons nor2 dec(3)" 

*column 1: baseline with reduced sample
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_if returned_days!=.
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD9C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if returned_days!=., absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(baseline) replace 
local i=`i'+1
*column 2: average days for return
gen x=returned_days/100
gen lndn13_x=lndn13*x
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(Avg_days) 
*column 3: col 2 + FIW
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD9C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(Avg_days) 
local i=`i'+1
drop x lndn13_x
*column 4: share of letters returned
gen x=Dreturned_any
gen lndn13_x=lndn13*x
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(Sh_returned) 
*column 5: column 4 + FIW
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD9C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(Sh_returned) 
local i=`i'+1
drop x lndn13_x
*column 6: share of letters returned in 90 days or less
gen x=Dreturned_90
gen lndn13_x=lndn13*x
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(Sh_returned90) 
*column 7: column 6 + FIW
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD9C7) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(Sh_returned90) tex(frag)
local i=`i'+1
drop x lndn13_x

///////////////////////////////////
////// Table D10: Corruption //////
///////////////////////////////////

use "${dir}\Data\master\Estimations", clear

//programs for bootstrap
capture program drop sigma_boot_if
program define sigma_boot_if, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if x!=., absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end

capture program drop sigma_boot_x
program define sigma_boot_x, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end

local out "outreg2 using "${tables}\table_D10", bracket nocons nor2 dec(3)" 

// columns 1-3: CPI from Transparency International
*column 1: baseline in CPI sample
gen x=cpi
gen lndn13_x=lndn13*x
capture program drop sigma_boot
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_if
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD10C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if x!=., absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(CPI) replace
local i=`i'+1
*column 2: CPI hetero
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(CPI) 
*column 3: CPI + FIW
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD10C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(CPI) 
local i=`i'+1
drop x lndn13_x
// columns 4-6: CCI from WB WGI
*column 4: baseline in CCI sample
gen x=cci
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_if
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD10C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if x!=., absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(CCI) 
local i=`i'+1
*column 5: CCI hetero
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(CCI) 
*column 6: CCI + FIW
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD10C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(CCI) tex(frag)
local i=`i'+1
drop x lndn13_x

///////////////////////////////////////////////////
////// Table D11: Other democracy measures /////////
///////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

//programs for bootstrap
capture program drop sigma_boot2
program define sigma_boot2, rclass
	reghdfe lngdp14 lndn13 x x2 lndn13_x, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_x]/_b[lndn13]
exit
end

capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 x lndn13_x, absorb(countrycode year) cluster(countrycode)
	sum x if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_x]/_b[lndn13]
exit
end

local out "outreg2 using "${tables}\table_D11", bracket nocons nor2 keep(lndn* fiw* D* x x2) dec(3)"

//column 1: Negative of Polity2 
gen x=-polity2
gen x2=x^2
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot2
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD11C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 x x2 lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(polity) replace
local i=`i'+1
drop x x2 lndn13_x
//column 2: Negative of Polity V democracy measure
gen x=-democracyP5
gen x2=x^2
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot2
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD11C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 x x2 lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(DemocP5)
local i=`i'+1
drop x x2 lndn13_x
//column 3: Polity V autocracy measure
gen x=autocracyP5
gen x2=x^2
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot2
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD11C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 x x2 lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(AutocP5)
local i=`i'+1
drop x x2 lndn13_x
//column 4: Negative of Voice and Accountability measure from WB WGI
gen x=-va
gen x2=x^2
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot2
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD11C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 x x2 lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(VA)
local i=`i'+1
drop x x2 lndn13_x
//column 5: Autocracy measure from DD (Cheibub et al)
gen x=autocracyDD
gen x2=x^2
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD11C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(AutocDD)
local i=`i'+1
drop x x2 lndn13_x
//column 6: Autocracy measure from Papaioanou
gen x=autocracyPS
gen x2=x^2
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD11C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(AutocPS)
local i=`i'+1
drop x x2 lndn13_x
//column 7: Autocracy measure from Acemoglu et al.
gen x=autocracyANRR
gen x2=x^2
gen lndn13_x=lndn13*x
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD11C7) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 x lndn13_x, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(AutocANRR) tex(frag)
local i=`i'+1
drop x x2 lndn13_x

////////////////////////////////////////////
////// Table D12: Other NTL measures ////////
////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

//program for bootstrap
capture program drop sigma_boot
program define sigma_boot, rclass
	args y
	reghdfe `y' lndn fiw fiw2 lndn_fiw, absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn_fiw]/_b[lndn]
exit
end

local out "outreg2 using "${tables}\table_D12", bracket nocons nor2 dec(3)"

*column 1: Henderson, Storeygard and Weil
gen lndn=lndnHSW
gen lndn_fiw=lndn*fiw
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot lngdpHSW
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD12C1) erows(ci_percentile ci_bc)
reghdfe lngdpHSW lndn fiw fiw2 lndn_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(HSW) replace
local i=`i'+1
drop lndn lndn_fiw
*column 2: Pinkosvkiy and Sala-i-Martin
gen lndn=lndnPS
gen lndn_fiw=lndn*fiw
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot lngdp14
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD12C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn fiw fiw2 lndn_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(PS)
local i=`i'+1
drop lndn lndn_fiw
*column 3: Hodler and Raschky
gen lndn=lndnHR
gen lndn_fiw=lndn*fiw
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot lngdp14
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD12C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn fiw fiw2 lndn_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(HR)
local i=`i'+1
drop lndn lndn_fiw
*column 4: Li et al
gen lndn=lndn18
gen lndn_fiw=lndn*fiw
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot lngdp_v2019_12
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD12C4) erows(ci_percentile ci_bc)
reghdfe lngdp_v2019_12 lndn fiw fiw2 lndn_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(LZZZ)
local i=`i'+1
drop lndn lndn_fiw
*column 5: Li et al (adjusted 2014)
gen lndn=lndn18_adj14
gen lndn_fiw=lndn*fiw
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot lngdp_v2019_12
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD12C5) erows(ci_percentile ci_bc)
reghdfe lngdp_v2019_12 lndn fiw fiw2 lndn_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(adj14)
local i=`i'+1
drop lndn lndn_fiw
*column 6: Li et al (adjusted 2014 and 2017)
gen lndn=lndn18_adj1417
gen lndn_fiw=lndn*fiw
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot lngdp_v2019_12
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD12C6) erows(ci_percentile ci_bc)
reghdfe lngdp_v2019_12 lndn fiw fiw2 lndn_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(adj1417) 
local i=`i'+1
drop lndn lndn_fiw
*column 7: lndn13 (adjusted 2010)
gen lndn=lndn13_adj10
gen lndn_fiw=lndn*fiw
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot lngdp14
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD12C7) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn fiw fiw2 lndn_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) ctitle(adj10) tex(frag)
local i=`i'+1
drop lndn lndn_fiw

///////////////////////////////////
////// Table D13: FIW fixed ////////
///////////////////////////////////

use "${dir}\Data\master\Estimations", clear

local out "outreg2 using "${tables}\table_D13", bracket nocons nor2 keep(lndn* fiw* D*) dec(3)"

capture program drop sigma_boot_x
program define sigma_boot_x, rclass
	args cond time
	reghdfe lngdp14 lndn13 lndn13_x if `cond', absorb(countrycode `time') cluster(countrycode)
	sum x if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_x]/_b[lndn13]
exit
end

capture program drop sigma_boot_dummy
program define sigma_boot_dummy, rclass
	args cond time
	reghdfe lngdp14 lndn13 lndn13_Dpfree_avg9213 lndn13_Dnfree_avg9213 if `cond', absorb(countrycode `time') cluster(countrycode)
	return scalar sigma = _b[lndn13_Dnfree_avg9213]/_b[lndn13]
exit
end

local cond_D13 fiw!=.

*columns 1-2: FIW average 92-13
gen x=fiw_avg9213
gen lndn13_x=lndn13*x
*column 1
bootstrap r(sigma), saving(${temp}\boot_TD13C1,replace) cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x `cond_D13' i.year
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD13C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 lndn13_x if `cond_D13', absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, Subregion x Year FE, No, Average, 92-13, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
local i=`i'+1
*column 2
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x `cond_D13' i.subsubregcode_un#i.year 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD13C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 lndn13_x if `cond_D13', absorb(countrycode i.subsubregcode_un#i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, No, Subregion x Year FE, Yes, Average, 92-13, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
drop x lndn13_x
*columns 3-4: FIW average 92-13: dummies
gen lndn13_Dpfree_avg9213=lndn13*Dpfree_avg9213
gen lndn13_Dnfree_avg9213=lndn13*Dnfree_avg9213
*column 3
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_dummy `cond_D13' i.year
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD13C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 lndn13_Dpfree_avg9213 lndn13_Dnfree_avg9213 if `cond_D13', absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, Subregion x Year FE, No, Average, 92-13, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 4
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_dummy `cond_D13' i.subsubregcode_un#i.year
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD13C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 lndn13_Dpfree_avg9213 lndn13_Dnfree_avg9213 if `cond_D13', absorb(countrycode i.subsubregcode_un#i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, Subregion x Year FE, No, Average, 92-13, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*columns 5-6: FIW average 82-91
gen x=fiw_avg8291
gen lndn13_x=lndn13*x
*column 5
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x `cond_D13' i.year
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD13C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 lndn13_x if `cond_D13', absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, Subregion x Year FE, No, Average, 82-91, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) 
local i=`i'+1
*column 6
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_x `cond_D13' i.subsubregcode_un#i.year 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD13C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 lndn13_x if `cond_D13', absorb(countrycode i.subsubregcode_un#i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, No, Subregion x Year FE, Yes, Average, 82-91, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)
local i=`i'+1
drop x lndn13_x

//////////////////////////////////////
////// Table D14: Growth rates ////////
//////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

gen dn13_growth_fiw=dn13_growth*fiw

capture program drop sigma_boot
program define sigma_boot, rclass
	args quad
	reghdfe gdp14_growth dn13_growth fiw `quad' dn13_growth_fiw, a(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[dn13_growth_fiw]/_b[dn13_growth]
exit
end

local out "outreg2 using "${tables}\table_D14", bracket nocons nor2 dec(3)"

*column 1: basic correlation
reghdfe gdp14_growth dn13_growth if fiw!=., a(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, Winsorization, 1-99) addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
*column 2: control for FIW
reghdfe gdp14_growth dn13_growth fiw, a(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, Winsorization, 1-99) addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 3: gradient
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD14C3) erows(ci_percentile ci_bc)
reghdfe gdp14_growth dn13_growth fiw dn13_growth_fiw, a(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, Winsorization, 1-99, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 4: quadratic
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot fiw2
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD14C4) erows(ci_percentile ci_bc)
reghdfe gdp14_growth dn13_growth fiw fiw2 dn13_growth_fiw, a(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, Winsorization, 1-99, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
drop dn13_growth gdp14_growth dn13_growth_fiw
*column 5: 2.5% winsorized on each side
sort countrycode year
gen dn13_growth=(avdn13-avdn13[_n-1])/avdn13[_n-1] if countrycode==countrycode[_n-1]
gen gdp14_growth=ny_gdp_mktp_kd_zg_2014_11/100
winsor2 gdp14_growth dn13_growth if year>=1992&year<=2013, replace cuts(2.5 97.5) 
gen dn13_growth_fiw=dn13_growth*fiw
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot fiw2
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD14C5) erows(ci_percentile ci_bc)
reghdfe gdp14_growth dn13_growth fiw fiw2 dn13_growth_fiw, a(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, Winsorization, 2.5-97.5, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
drop dn13_growth gdp14_growth dn13_growth_fiw
*column 6: 5% winsorized on each side
sort countrycode year
gen dn13_growth=(avdn13-avdn13[_n-1])/avdn13[_n-1] if countrycode==countrycode[_n-1]
gen gdp14_growth=ny_gdp_mktp_kd_zg_2014_11/100
winsor2 gdp14_growth dn13_growth if year>=1992&year<=2013, replace cuts(5 95)
gen dn13_growth_fiw=dn13_growth*fiw
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot fiw2
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD14C6) erows(ci_percentile ci_bc)
reghdfe gdp14_growth dn13_growth fiw fiw2 dn13_growth_fiw, a(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, Winsorization, 5-95, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)
local i=`i'+1
drop dn13_growth gdp14_growth dn13_growth_fiw

//////////////////////////////////////////////
////// Table D15: Specification checks ///////
//////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

*mata set-up
mata: mata set matafavor speed, perm

//variable creation
sort countrycode year
*lags
gen lndn13_l1=lndn13[_n-1] if countrycode==countrycode[_n-1]
gen lngdp14_l1=lngdp14[_n-1] if countrycode==countrycode[_n-1]
*first diff
gen d_lngdp14=lngdp14-lngdp14[_n-1] if countrycode==countrycode[_n-1]
gen d_lndn13=lndn13-lndn13[_n-1] if countrycode==countrycode[_n-1]
winsor2 d_lngdp14 d_lndn13 if year>=1992&year<=2013, replace cuts(1 99)
*interaction
gen d_lndn13_fiw=d_lndn13*fiw

capture program drop sigma_boot
program define sigma_boot, rclass
	args time
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode `time') cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end

capture program drop sigma_boot_chg
program define sigma_boot_chg, rclass
	args control
	reghdfe d_lngdp14 d_lndn13 `control' fiw fiw2 d_lndn13_fiw, absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[d_lndn13_fiw]/_b[d_lndn13]
exit
end

local out "outreg2 using "${tables}\table_D15", bracket nocons nor2 dec(3) keep(lndn* lngdp* d_* fiw fiw2)"

*column 1: subreg x year
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot i.subsubregcode_un#i.year
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD15C1) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode i.subsubregcode_un#i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
local i=`i'+1
*column 2: country trend
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot "year c.year#i.countrycode_num"
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD15C2) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode year c.year#i.countrycode_num) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 3: lagged lights
capture program drop sigma_boot
program define sigma_boot, rclass
	args time
	reghdfe lngdp14 lndn13 lndn13_l1 fiw fiw2 lndn13_fiw, absorb(countrycode i.year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+_b[lndn13_l1])
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot  
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD15C3) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 lndn13_l1 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) 
local i=`i'+1
*column 4: logs: first diff and basic correlation
reghdfe d_lngdp14 d_lndn13 if fiw!=., absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes) addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 5: logs: first diff and main spec
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_chg
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD15C5) erows(ci_percentile ci_bc)
reghdfe d_lngdp14 d_lndn13 fiw fiw2 d_lndn13_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 6: logs: first diff+lagged GDP
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_chg lngdp14_l1
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD15C6) erows(ci_percentile ci_bc)
reghdfe d_lngdp14 lngdp14_l1 d_lndn13 fiw fiw2 d_lndn13_fiw, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 7: logs: first diff+lagged GDP+GMM
xtset countrycode_num year
xtabond2 d_lngdp14 lngdp14_l1 d_lndn13 fiw fiw2 d_lndn13_fiw i.countrycode_num i.year, gmm(lngdp14_l1 d_lndn13 fiw fiw2 d_lndn13_fiw, collapse) small iv(i.countrycode_num i.year) cluster(countrycode)
sum fiw if e(sample),det
local iqr: di %2.1f r(p75)-r(p25)
nlcom sigma: `iqr'*_b[d_lndn13_fiw]/_b[d_lndn13]
mat a = r(b)
local sigma: di %4.3f a[1,1]
mat b = r(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE,[`sigmaSE'], sigma 95 CI, [0.21 2.01]) tex(frag)
nlcom sigma: `iqr'*_b[d_lndn13_fiw]/_b[d_lndn13], post
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD15C7)
local i=`i'+1
xtset,clear

///////////////////////////////////////
////// Table D16: ZAM influence ///////
///////////////////////////////////////

use "${dir}\Data\temp\ZAM_sample_in.dta",clear

*Combine with output from ZAMinfluence package in R
merge 1:1 countrycode year using "${temp}\ZAM_sample_out.dta",keepusing(out_zam*)
drop _merge

local out "outreg2 using "${tables}\table_D16", bracket nocons nor2 keep(lndn* fiw*) dec(3)"

//Program for bootstrap
capture program drop sigma_boot
program define sigma_boot, rclass
	args excl
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if `excl', absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end

*column 1: sample to lose significance
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot out_zam_sig==0
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if out_zam_sig==0, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
*column 2: sample to flip sign
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot out_zam_sign==0
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if out_zam_sign==0, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 3: sample to flip sign significantly
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot out_zam_both==0
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if out_zam_both==0, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within))
*column 4: drop top 4 countries in ZAM sample
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot `"countrycode!="LBR"&countrycode!="AGO"&countrycode!="KIR"&countrycode!="TUV""'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD16C4) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if countrycode!="LBR"&countrycode!="AGO"&countrycode!="KIR"&countrycode!="TUV", absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 5: drop top 8 countries in ZAM sample
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot `"countrycode!="LBR"&countrycode!="AGO"&countrycode!="KIR"&countrycode!="TUV"&countrycode!="AZE"&countrycode!="BLR"&countrycode!="SLB"&countrycode!="TZA""'
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD16C5) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if countrycode!="LBR"&countrycode!="AGO"&countrycode!="KIR"&countrycode!="TUV"&countrycode!="AZE"&countrycode!="BLR"&countrycode!="SLB"&countrycode!="TZA", absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 6: drop top autocracies
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot fiw<5.5
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD16C6) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if fiw<5.5, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within))
local i=`i'+1
*column 7: drop top years in ZAM sample
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot year>=1994&year<=2009
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
local drop: di %4.3f 1-(e(N)/3895)
parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(TD16C7) erows(ci_percentile ci_bc)
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if year>=1994&year<=2009, absorb(countrycode year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high'], Pct dropped, `drop') addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)
local i=`i'+1

//Figure A11: countries in excluded ZAM sample
preserve
collapse (sum) out_zam_sig,by( countrycode countryname Dfree_avg9213 Dpfree_avg9213 Dnfree_avg9213 )
gsort -out_zam_sig countryname
gen id=[_n]
labmask id, values(countryname)
twoway (bar out_zam_sig id if Dfree_avg9213==1, fcolor(green%30)) (bar out_zam_sig id if Dpfree_avg9213==1, fcolor(yellow%60)) (bar out_zam_sig id if Dnfree_avg9213==1, fcolor(red)) if out_zam_sig>0, ytitle(Number of Observations Excluded) ylabel(,angle(horizontal)) xtitle("") xlabel(1(1)65, labsize(tiny) angle(forty_five) valuelabel) legend(order(1 "Free" 2 "Partially Free" 3 "Not Free") rows(1)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(ctrys_zam,replace)
graph export "${figures}\figure_A11.pdf", as(pdf) replace
restore

//Figure A12: Characteristics of excluded ZAM sample
preserve
	gen fiw_round=round(fiw)
	collapse out_zam_sig,by(fiw_round)
	twoway (bar out_zam_sig fiw if fiw==0, barwidth(0.8) color(green%70)) (bar out_zam_sig fiw if fiw==1, barwidth(0.8) color(green%30)) (bar out_zam_sig fiw if fiw==2, barwidth(0.8) color(yellow%20)) (bar out_zam_sig fiw if fiw==3, barwidth(0.8) color(yellow%50)) (bar out_zam_sig fiw if fiw==4, barwidth(0.8) color(orange%80)) (bar out_zam_sig fiw if fiw==5, barwidth(0.8) color(orange_red%80)) (bar out_zam_sig fiw if fiw==6, barwidth(0.8) color(red%90)), ytitle(Share of Observations Excluded) ylabel(,angle(horizontal)) xtitle(Freedom in the World Index) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) legend(off) name(fiw_zam,replace)
graph export "${figures}\figure_A12a.pdf", as(pdf) replace
restore

preserve
	gen fiw_round=round(fiw)
	collapse out_zam_sig,by(year)
	twoway (connected out_zam_sig year,mcolor(navy)), ytitle(Share of Observations Excluded) ylabel(,angle(horizontal)) xtitle("Year") xlabel(1992(2)2012) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(year_zam,replace)
graph export "${figures}\figure_A12b.pdf", as(pdf) replace
restore

//Figure D5: Excluding 1000 1% subsamples from ZAM
set seed ${seed}
forvalues x=1/1000{
	gen drop_`x'=0
	replace drop_`x'=runiform() if out_zam_sig==1
	gsort -drop_`x'
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if [_n]>=41, absorb(countrycode year) cluster(countrycode)
	local drop: di %4.3f 1-(e(N)/3895)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	nlcom sigma: `iqr'*_b[lndn13_fiw]/_b[lndn13],post
	parmest,saving(${temp}\drop_zam`x',replace) idnum(`drop')
}
drop drop*

preserve
	clear
	forvalues x=1/1000{
		append using ${temp}\drop_zam`x'
	}
	gen id=_n
	twoway (rcap min95 max95 id)(scatter estimate id,msize(small)), ytitle(Estimated {&sigma}) ylabel(, angle(horizontal)) xtitle("Subsample (1% Drop)") xlabel(0(100)1000) legend(order(1 "Point estimate" 2 "95% confidence interval")) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(zam_sim,replace)
graph export "${figures}\figure_D5.pdf", as(pdf) replace
restore

///////////////////////////////////////////////////////////////
////// Table D17: Sub-components controlling for shares ///////
///////////////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

local out "outreg2 using "${tables}\table_D17", bracket nocons nor2 keep(lndn* fiw* D*) dec(3)"
local cond_sub cons_gdp!=.&inv_gdp!=.&gov_gdp!=.&exp_gdp!=.&imp_gdp!=.

capture program drop sigma_boot_sub
program define sigma_boot_sub, rclass
	args y cond
	reghdfe `y' lndn13 fiw fiw2 cons_gdp inv_gdp gov_gdp exp_gdp lndn13_fiw if `cond', absorb(countrycode i.year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end

*column 1: consumption
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_sub lncons `cond_sub' 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
reghdfe lncons lndn13 fiw fiw2 cons_gdp inv_gdp gov_gdp exp_gdp lndn13_fiw if `cond_sub', absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) replace
*column 2: investment
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_sub lninv `cond_sub' 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
reghdfe lninv lndn13 fiw fiw2 cons_gdp inv_gdp gov_gdp exp_gdp lndn13_fiw lndn13_fiw if `cond_sub', absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) 
*column 3: gvt spending
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_sub lngov `cond_sub' 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
reghdfe lngov lndn13 fiw fiw2 cons_gdp inv_gdp gov_gdp exp_gdp lndn13_fiw if `cond_sub', absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) 
*column 4: exports
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_sub lnexp `cond_sub' 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
reghdfe lnexp lndn13 fiw fiw2 cons_gdp inv_gdp gov_gdp exp_gdp lndn13_fiw if `cond_sub', absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) 
*column 5: imports
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot_sub lnimp `cond_sub' 
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
mat c = e(ci_bc)
local sigma_95_low: di %3.2f c[1,1]
local sigma_95_high: di %3.2f c[2,1]
reghdfe lnimp lndn13 fiw fiw2 cons_gdp inv_gdp gov_gdp exp_gdp lndn13_fiw if `cond_sub', absorb(countrycode i.year) cluster(countrycode)
`out' addtext(Country FE, Yes, Year FE, Yes, sigma,`sigma', sigma SE, [`sigmaSE'], sigma 95 CI, [`sigma_95_low' `sigma_95_high']) addstat(Countries, e(N_clust), R-squared, e(r2_within)) tex(frag)

/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
//////////////////// APPENDIX FIGURES ////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////

/////////////////////////////////
////// Figures A1/A3: Maps //////
/////////////////////////////////

use "${dir}\Data\master\Estimations", clear
egen temp=mean(lndn13_lr) if year==1992|year==1993,by(countrycode)
egen lndn13_9293=mean(temp),by(countrycode)
egen temp1=mean(lndn13_lr) if year==2012|year==2013,by(countrycode)
egen lndn13_1213=mean(temp1),by(countrycode)
drop temp*
gen d_lndn13_lr=lndn13_1213-lndn13_9293
collapse fiw d_fiw_lr9213 avdn13 d_lndn13_lr if year>=1992&year<=2013,by(countrycode countryname)
tab avdn13
tempfile merge_fiw
save `merge_fiw'
use "${dir}\Data\master\world_map", clear
merge m:1 countrycode using `merge_fiw'	
drop if _merge==2 //No Hong Kong, Macau, Taiwan or Kosovo in shapefile
drop _merge
gen d_fiw_round=round(d_fiw,0.5)
*I round larger falls/increases to -1 or +1
replace d_fiw_round = -1 if d_fiw_round<-1&d_fiw_round<.
replace d_fiw_round = 1 if d_fiw_round>1&d_fiw_round<.
sort _ID
lab define d_fiw -1 "{&le}-1" 1 "{&ge}1"
lab values d_fiw_round d_fiw 
*average FIW
spmap fiw using "${dir}\Data\master\world_map_shp", id(_ID) clmethod(custom) clbreaks(0(1)6) fcolor(Heat) osize(vthin vthin vthin vthin vthin vthin) legstyle(2) legend(symy(*2) symx(*2) size(medium) region(lstyle(foreground) fcolor(white))) ndfcolor(gs8) ndlabel(N/A) name(fiw_avg_map,replace)
graph export "${figures}\figure_A1a.pdf", as(pdf) replace
*change in FIW
spmap d_fiw_round using "${dir}\Data\master\world_map_shp", id(_ID) clmethod(unique) fcolor(BuYlRd) osize(vthin vthin vthin vthin vthin vthin) legstyle(1) legend(symy(*2) symx(*2) size(medium) region(lstyle(foreground) fcolor(white))) ndfcolor(gs8) ndlabel(N/A) name(d_fiw_map,replace)
graph export "${figures}\figure_A1b.pdf", as(pdf) replace
*average DN
spmap avdn13 using "${dir}\Data\master\world_map_shp", id(_ID) clmethod(custom) clbreaks(0 0.20 1 2.5 10 63) fcolor(PuRd) osize(vthin vthin vthin vthin vthin vthin) legstyle(2) legend(symy(*2) symx(*2) size(medium) region(lstyle(foreground) fcolor(white))) ndfcolor(gs8) ndlabel(N/A) name(avdn_avg_map,replace)
graph export "${figures}\figure_A3a.pdf", as(pdf) replace
*change in DMSP NTL
spmap d_lndn13_lr using "${dir}\Data\master\world_map_shp", id(_ID) clmethod(custom) clbreaks(-0.5 0 0.5 0.75 1 3) fcolor(sienna sand bluishgray eltgreen emerald) osize(vthin vthin vthin vthin vthin vthin) legstyle(2) legend(symy(*2) symx(*2) size(medium) region(lstyle(foreground) fcolor(white))) ndfcolor(gs8) ndlabel(N/A) name(d_lndn13_lr_map,replace)
graph export "${figures}\figure_A3b.pdf", as(pdf) replace

////////////////////////////////////////
////// Figure A2: NTL comparison //////
////////////////////////////////////////

// panel (a): NTL data sources

use "${dir}\Data\master\ntl_sources", clear

twoway (rcap min_year max_year id if id<=6,horizontal lcolor(navy%50)) (rcap min_year max_year id if id==7,horizontal lcolor(red%80)), ylabel(1(1)7,valuelabel angle(horizontal)) ytitle(Satellite) xlabel(1992(2)2018,angle(45)) xtitle(Years with Available Data) legend(order(1 "DMSP-OLS" 2 "VIIRS")) name(ntl_data,replace)
graph export "${figures}\figure_A2a.pdf", as(pdf) replace

// panel (b): Comparison of NTL data sources

use "${dir}\Data\master\Estimations", clear

sort countrycode year

keep if (year<=2008&lndn13!=.&lndnHSW!=.&lndnHR!=.&lndn18!=.)|(year>=2008&year<=2013&lndnHR!=.&lndn13!=.&lndn18!=.)|(year>=2014)

gen d_lndn18=lndn18-lndn18[_n-1] if countrycode==countrycode[_n-1]
gen d_lndn13=lndn13-lndn13[_n-1] if countrycode==countrycode[_n-1]
gen d_lndnHSW=lndnHSW-lndnHSW[_n-1] if countrycode==countrycode[_n-1]
gen d_lndnHR=lndnHR-lndnHR[_n-1] if countrycode==countrycode[_n-1]

collapse d_lndn* if year>=1992&year<=2018&fiw!=.,by(year)

twoway (connected d_lndn18 year,mcolor(navy)) (connected d_lndn13 year,mcolor(gold)) (connected d_lndnHSW year,mcolor(red%50)) (connected d_lndnHR year,mcolor(green%30)), ylabel(,angle(horizontal) format(%2.1f)) ytitle(Average change in ln NTL DN) xlabel(1990(5)2020) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) legend(order(1 "Li et al. (2020)" 2 "DMSP (own calculations)" 3 "Henderson et al. (2012)" 4 "Hodler & Raschky (2014)")) name(mean_dn,replace)
graph export "${figures}\figure_A2b.pdf", as(pdf) replace


///////////////////////////////////////////////
////// Figure A4: NTL in 1992 and 2013 ////////
///////////////////////////////////////////////

*Produced in QGIS

//////////////////////////////////////////////////////
////// Figure A5: Country-specific elasticities //////
//////////////////////////////////////////////////////

*Figure is produced with Figure 2 above

///////////////////////////////////////////////////////////////////////
////// Figure A6: Distribtion of Bootstrap estimates from T1C4 ////////
///////////////////////////////////////////////////////////////////////

use ${temp}\boot_T1C4.dta,clear

swilk _bs_1
local wilk: di %4.3f r(p)
sfrancia _bs_1
local fran: di %4.3f r(p)
qnorm _bs_1,ylabel(,angle(horizontal)) ytitle(Bootstrapped Sigma Estimates) caption("Shapiro-Wilk p-value: `wilk'" "Shapiro-Francia p-value: `fran'", pos(10) ring(0)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(boot_norm,replace)
graph export "${figures}\figure_A6.pdf", as(pdf) replace

////////////////////////////////////////////
////// Figure A7: Year-specific sigma //////
////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

//determine balanced sample
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
gen in_sample=e(sample)
egen tot_sample=sum(in_sample),by(countrycode)
//estimate main regression in balanced sample
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if tot_sample==22, absorb(countrycode year) cluster(countrycode)
sum fiw if e(sample),det
local iqr: di %2.1f r(p75)-r(p25)
nlcom sigma: `iqr'*_b[lndn13_fiw]/_b[lndn13]	//Delta method is ok because I am only using the point estimate of sigma
mat a = r(b)
local sigma_mean: di %4.3f a[1,1]
*bootstrap
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 c.lndn13#i.year c.lndn13_fiw#i.year if tot_sample==22, absorb(countrycode year c.fiw#i.year c.fiw2#i.year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	forvalues y=1992/2013{
		return scalar sigma`y' = `iqr'*_b[`y'.year#c.lndn13_fiw]/_b[`y'.year#c.lndn13]
	}
	exit
end
bootstrap r(sigma1992) r(sigma1993) r(sigma1994) r(sigma1995) r(sigma1996) r(sigma1997) r(sigma1998) r(sigma1999) r(sigma2000) r(sigma2001) r(sigma2002) r(sigma2003) r(sigma2004) r(sigma2005) r(sigma2006) r(sigma2007) r(sigma2008) r(sigma2009) r(sigma2010) r(sigma2011) r(sigma2012) r(sigma2013), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
parmest,saving(${temp}\year_gradient,replace) erows(ci_percentile ci_bc)
*draw graph
preserve
clear
use ${temp}\year_gradient,clear
gen year=1991+[_n]
twoway (scatter estimate year, mcolor(navy)) (rcap er_2_1 er_2_2 year), ytitle(Year-specific estimate of {&sigma}) ylabel(, angle(horizontal)) yline(`sigma_mean',lpattern(dash) lcolor(maroon)) xtitle("") xlabel(1992(2)2012) legend(off) scheme(sj) name(sigma_yr, replace) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
graph export "${figures}\figure_A7.pdf", as(pdf) replace
restore

//////////////////////////////////////////////////////////////////////////
////// Figure A8: Disaggregate estimates of gradient for each FiW ////////
//////////////////////////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

*create rounded version of FIW, create dummies and interact w/ lights
gen fiw_round=round(fiw)
forvalues x=0/6{
	gen Dfiw_`x'=(fiw_round==`x') if fiw_unadj!=.
	gen lndn13_Dfiw_`x'=lndn13*Dfiw_`x'
}
*run regression for figure
reghdfe lngdp14 lndn13_Dfiw_0 lndn13_Dfiw_1 lndn13_Dfiw_2 lndn13_Dfiw_3 lndn13_Dfiw_4 lndn13_Dfiw_5 lndn13_Dfiw_6,absorb(countrycode i.year fiw_round) cluster(countrycode) nocons
parmest,saving(${temp}\fiw_levels,replace)
*bootstrapped estimates of sigma for bottom
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13_Dfiw_0 lndn13_Dfiw_1 lndn13_Dfiw_2 lndn13_Dfiw_3 lndn13_Dfiw_4 lndn13_Dfiw_5 lndn13_Dfiw_6,absorb(countrycode i.year fiw_round) cluster(countrycode) nocons
	forvalues x=1/6{
		return scalar sigma`x' = (_b[lndn13_Dfiw_`x']-_b[lndn13_Dfiw_0])/_b[lndn13_Dfiw_0]
	}
exit
end
bootstrap r(sigma1) r(sigma2) r(sigma3) r(sigma4) r(sigma5) r(sigma6), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
forvalues x=1/6{
	local sigma`x': di %4.3f a[1,`x']	
}
mat b = e(V)
forvalues x=1/6{
	local sigmaSE`x': di %4.3f sqrt(b[`x',`x'])	
}
*draw graph
preserve
use "${temp}\fiw_levels",clear
gen fiw=_n-1
twoway (bar estimate fiw if fiw==0, barwidth(0.8) color(green%70)) (bar estimate fiw if fiw==1, barwidth(0.8) color(green%30)) (bar estimate fiw if fiw==2, barwidth(0.8) color(yellow%20)) (bar estimate fiw if fiw==3, barwidth(0.8) color(yellow%50)) (bar estimate fiw if fiw==4, barwidth(0.8) color(orange%80)) (bar estimate fiw if fiw==5, barwidth(0.8) color(orange_red%80)) (bar estimate fiw if fiw==6, barwidth(0.8) color(red%90)) (rcap min95 max95 fiw ,lcolor(gs8)), ytitle(NTL Elasticity of GDP) ylabel(0(0.1)0.5, angle(horizontal)) xtitle(Freedom in the World Index) xlabel(0 1 `" `"1"' `"{&sigma}: `sigma1'"'  `"(`sigmaSE1')"' "' 2 `" `"2"' `"{&sigma}: `sigma2'"'  `"(`sigmaSE2')"' "' 3 `" `"3"' `"{&sigma}: `sigma3'"'  `"(`sigmaSE3')"' "' 4 `" `"4"' `"{&sigma}: `sigma4'"'  `"(`sigmaSE4')"' "' 5 `" `"5"' `"{&sigma}: `sigma5'"'  `"(`sigmaSE5')"' "' 6 `" `"6"' `"{&sigma}: `sigma6'"'  `"(`sigmaSE6')"' "' , labsize(small)) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fh_levels,replace)
graph export "${figures}\figure_A8.pdf", as(pdf) replace
restore

///////////////////////////////////////////////
////// Figure A9: Regime subcategories ////////
///////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

*variable creation
foreach x of numlist 0/3{
	gen lndn13_r`x'=cond(regime_adj==`x',lndn13,0) if regime_adj!=.
}
bysort regime_adj: sum fiw if lndn13!=.&lngdp14!=.&year>=1992&year<=2008 //I added manually these numbers to the figure below
*run regression
reghdfe lngdp14 ibn.regime_adj lndn13_r0-lndn13_r3 if fiw!=.,absorb(countrycode i.year) cluster(countrycode) nocons
parmest,saving(${temp}\regime,replace)
*bootstrap
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 ibn.regime_adj lndn13_r0-lndn13_r3 if fiw!=.,absorb(countrycode i.year) cluster(countrycode) nocons
	forvalues x=1/3{
		return scalar sigma`x' = (_b[lndn13_r`x']-_b[lndn13_r0])/_b[lndn13_r0]
	}
exit
end
bootstrap r(sigma1) r(sigma2) r(sigma3), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p 
mat a = e(b)
forvalues x=1/3{
	local sigma`x': di %4.3f a[1,`x']	
}
mat b = e(V)
forvalues x=1/3{
	local sigmaSE`x': di %4.3f sqrt(b[`x',`x'])	
}
*draw graph
preserve
	use ${temp}\regime ,clear
	keep in 5/8
	gen id=_n
	twoway (bar estimate id if id<=2,barw(0.5) fcolor(green%50)) (bar estimate id if id>=3,barw(0.5) fcolor(red%50)) (rcap min95 max95 id), xlabel(1 `" `"Parliamentary"' `"Democracy"' `"[Avg. FIW: 0.72]"' "' 2 `" `"Presidential"' `"Democracy"' `"[Avg. FIW: 1.47]"' `"{&sigma}: `sigma1'"'  `"(`sigmaSE1')"' "' 3 `" `"Civilian"' `"Dictatorship"' `"[Avg. FIW: 4.38]"' `"{&sigma}: `sigma2'"'  `"(`sigmaSE2')"' "' 4 `" `"Military/Royal"' `"Dictatorship"' `"[Avg. FIW: 4.64]"' `"{&sigma}: `sigma3'"'  `"(`sigmaSE3')"' "', labsize(small)) ytitle("NTL Elasticity of GDP") ylabel(0(0.1)0.4) xtitle("") xscale(range(0.5/4.5)) legend(off) ylabel(,angle(horizontal)) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(regime_adj,replace)
	graph export "${figures}\figure_A9.pdf", as(pdf) replace
restore

/////////////////////////////////////////
////// Figure A10: Data revisions ////////
/////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
egen ctry_sample=max(e(sample)),by(countrycode)

keep countryname countrycode year ctry_sample fiw lngdp_v2014_04 lngdp_v2015_04 lngdp_v2016_04 lngdp_v2017_04 lngdp_v2018_04 lngdp_v2019_04 lngdp_v2020_04 lngdp_v2021_04 

gen gdp_revision=.
forvalues x=2015/2021{
	local y=`x'-1
	gen d_lngdp`x'=lngdp_v`x'_04-lngdp_v`y'_04
	egen avg_d_lngdp`x'=mean(d_lngdp`x'),by(countrycode)
	replace avg_d_lngdp`x'=round(avg_d_lngdp`x',0.01)
	replace gdp_revision=0 if year==`x'&avg_d_lngdp`x'!=.
	replace gdp_revision=1 if gdp_revision==0&year==`x'&(avg_d_lngdp`x'>0.1|avg_d_lngdp`x'<-0.1)
}

preserve
	collapse avg* if ctry_sample==1,by(countrycode)
	reshape long avg_d_lngdp,i(countrycode) j(vintage)
	sum avg,det
	tab avg
restore

reghdfe gdp_revision fiw if year>=2015&year<=2019&ctry_sample==1, absorb(countrycode year) nocons cluster(countrycode)
mat a = e(b)
local coef_panel: di %3.2f a[1,1]	
mat b = e(V)
local se_panel: di %3.2f sqrt(b[1,1])	

collapse fiw (max) gdp_revision if year>=2015&year<=2019&ctry_sample==1,by(countrycode)
reg gdp_revision fiw, robust
mat a = e(b)
local coef_xsection: di %3.2f a[1,1]	
mat b = e(V)
local se_xsection: di %3.2f sqrt(b[1,1])	

binscatter gdp_revision fiw, line(lfit) nodraw
disp `"`e(graphcmd)'"'
twoway (scatteri .5128205128205128 0 .5714285714285714 .2428571475403649 .7 .620000010728836 .7142857142857143 .992857141154153 .8 1.239999985694885 .5 1.5, mfcolor(green%30) mlcolor(black) msize(medlarge) msymbol(circle)) (scatteri .75 1.975000003973643 .5714285714285714 2.128571374075753 .5555555555555556 2.48888889948527 .7 2.990000009536743 .75 3.450000017881393 .6363636363636364 3.936363610354337, mfcolor(yellow%60) mlcolor(black) msize(medlarge) msymbol(triangle)) (scatteri 1 4.444444444444445 .5555555555555556 4.766666730244954 .9090909090909091 5.218181870200417 .5 5.5 .1428571428571428 5.957142898014614, mfcolor(red) mlcolor(black) msize(medlarge) msymbol(square)) (function  0*x^2+.0120728021712954*x+.6023655174824125, range(0 5.957142898014614) lcolor(gs10) lpattern(solid)), scheme(sj) graphregion(fcolor(white))  xtitle(fiw) ytitle(estimate) legend(order(1 "Free" 2 "Partially Free" 3 "Not Free") r(1) pos(11) ring(0)) ylabel(,angle(horizontal)) ytitle(GDP Revision (=1)) xtitle(Freedom in the World Index) caption("Cross-Sectional Regression: `coef_xsection' [`se_xsection']" "Panel Regression: `coef_panel' [`se_panel']", pos(8) ring(0) box) name(scatter_gdp_rev,replace)
graph export "${figures}\figure_A10.pdf", as(pdf) replace

////////////////////////////////////////////////////////////////////////////////
////// Figures A11 and A12: Characteristics of 5% ZAMinfluence subsample ///////
////////////////////////////////////////////////////////////////////////////////

*Produced above with Table D16

/////////////////////////////////////////////////////////////
////// Figures A13: Event studies with Li et al data ////////
/////////////////////////////////////////////////////////////

//into dictatorship

use "${dir}\Data\master\Estimations", clear

sort countrycode year

* I impute the FH autocracy measure for 2016-2018  
gen autocracyFH_fill=autocracyFH
replace autocracyFH_fill=1 if year>2015&fiw>=4&fiw<.
replace autocracyFH_fill=0 if year>2015&fiw<=2
replace autocracyFH_fill=autocracyFH_fill[_n-1] if year>2015&autocracyFH_fill[_n-1]!=.&autocracyFH_fill==.&countrycode==countrycode[_n-1]

*create transition events and remove unbalanced spells (adjusting based on total leads and lags can be misleading because some countries have multiple events) 
gen aut_in_lag0=autocracyFH_fill==1&autocracyFH_fill[_n-1]==0&countrycode==countrycode[_n-1] if year>=1992&year<=2018&autocracyFH_fill!=.
replace aut_in_lag0=0 if aut_in_lag0==1&(countrycode=="BGD"|countrycode=="DOM"|countrycode=="MDV"|countrycode=="PHL"|countrycode=="SLE") //<4 lags
replace aut_in_lag0=0 if aut_in_lag0==1&(countrycode=="DJI"|countrycode=="GMB"|countrycode=="MRT") //<4 leads
replace aut_in_lag0=0 if aut_in_lag0==1&(countrycode=="BIH"|(countrycode=="CAF"&year==2008)|countrycode=="FJI"|countrycode=="PER"|(countrycode=="GEO"&year==2003)|(countrycode=="GNB"&year==2003)|countrycode=="NER")	//short events in multi-event countries
replace aut_in_lag0=0 if aut_in_lag0==1&((countrycode=="HND"&year==2015)|countrycode=="LBY"|(countrycode=="THA"&year==2014)|countrycode=="TUR"|(countrycode=="PAK"&year==2018))
list countryname year if aut_in_lag0==1
*create leads and lags
forvalues x=1/25{
	local y=`x'-1
	gen aut_in_lag`x'=aut_in_lag`y'==0&aut_in_lag`y'[_n-1]==1&countrycode==countrycode[_n-1]&year==year[_n-1]+1 if year>=1992&year<=2018&autocracyFH_fill!=.
	replace aut_in_lag`x'=0 if autocracyFH_fill==0
}
gen aut_in_lead1=aut_in_lag0[_n+1]==1&aut_in_lag0==0&countrycode==countrycode[_n+1]&year==year[_n+1]-1 if year>=1992&year<=2018&autocracyFH_fill!=.
forvalues x=2/25{
	local y=`x'-1
	gen aut_in_lead`x'=aut_in_lead`y'==0&aut_in_lead`y'[_n+1]==1&countrycode==countrycode[_n+1]&year==year[_n+1]-1 if year>=1992&year<=2018&autocracyFH_fill!=.
	replace aut_in_lead`x'=0 if autocracyFH_fill==1
}
*create endpoint dummies at -9 or less and +9 or more
egen aut_in_lag9p=rowtotal(aut_in_lag9 - aut_in_lag25),missing
egen aut_in_lead9p=rowtotal(aut_in_lead9 - aut_in_lead25),missing
*interact leads and lags with lights
foreach x of varlist aut_in_lead9p aut_in_lead8 aut_in_lead7 aut_in_lead6 aut_in_lead5 aut_in_lead4 aut_in_lead3 aut_in_lead2 aut_in_lead1 aut_in_lag0 aut_in_lag1 aut_in_lag2 aut_in_lag3 aut_in_lag4 aut_in_lag5 aut_in_lag6 aut_in_lag7 aut_in_lag8 aut_in_lag9p {
	gen lndn18_`x'=lndn18_adj1417*`x'
}
*I create a dummy for autocracy observations not in these spells
egen temp=rowtotal(aut_in_lag0 aut_in_lag1 aut_in_lag2 aut_in_lag3 aut_in_lag4 aut_in_lag5 aut_in_lag6 aut_in_lag7 aut_in_lag8 aut_in_lag9p),miss
gen autocracyFH_other=autocracyFH_fill-temp
gen lndn18_autocracyFH_other=lndn18_adj1417*autocracyFH_other
drop temp
*regression
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp_v2020_12 aut_in_lead9p aut_in_lead8 aut_in_lead7 aut_in_lead6 aut_in_lead5 aut_in_lead4 aut_in_lead3 aut_in_lead2 aut_in_lead1 aut_in_lag0 aut_in_lag1 aut_in_lag2 aut_in_lag3 aut_in_lag4 aut_in_lag5 aut_in_lag6 aut_in_lag7 aut_in_lag8 aut_in_lag9p autocracyFH_other lndn18_adj1417 lndn18_autocracyFH_other lndn18_aut_in_lead9p-lndn18_aut_in_lag9p if fiw!=., absorb(countrycode year) cluster(countrycode)
	return scalar sigma = _b[lndn18_autocracyFH_other]/_b[lndn18_adj1417]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
reghdfe lngdp_v2020_12 aut_in_lead9p aut_in_lead8 aut_in_lead7 aut_in_lead6 aut_in_lead5 aut_in_lead4 aut_in_lead3 aut_in_lead2 aut_in_lead1 aut_in_lag0 aut_in_lag1 aut_in_lag2 aut_in_lag3 aut_in_lag4 aut_in_lag5 aut_in_lag6 aut_in_lag7 aut_in_lag8 aut_in_lag9p autocracyFH_other lndn18_adj1417 lndn18_autocracyFH_other lndn18_aut_in_lead9p-lndn18_aut_in_lag9p if fiw!=., absorb(countrycode year) cluster(countrycode)
parmest,saving(${temp}\autoc_in_event18,replace)
*draw graph
preserve
use ${temp}\autoc_in_event18,clear
keep in 24/40
gen id=0 in 9
replace id=id[_n-1]+1 in 10/17
replace id=[_n]-9 in 1/8
sort id
twoway (scatter estimate id,mcolor(red%90)) (rcap min95 max95 id), ytitle(Change in NTL elasticity of GDP) yline(0) ylabel(-0.1(0.05)0.15, angle(horizontal) format(%3.2f)) xtitle(Years since becoming Autocracy) xlabel(-8(2)8) xline(0, lpattern(dash)) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) note(Other autocracy {&sigma}: `sigma' (`sigmaSE')) name(event_autoc_in18,replace)
graph export "${figures}\figure_A13a.pdf", as(pdf) replace
restore

//out of dictatorship

use "${dir}\Data\master\Estimations", clear

sort countrycode year

* I impute the FH autocracy measure for 2016-2018  
gen autocracyFH_fill=autocracyFH
replace autocracyFH_fill=1 if year>2015&fiw>=4&fiw<.
replace autocracyFH_fill=0 if year>2015&fiw<=2
replace autocracyFH_fill=autocracyFH_fill[_n-1] if year>2015&autocracyFH_fill[_n-1]!=.&autocracyFH_fill==.&countrycode==countrycode[_n-1]

*create transition events and remove unbalanced spells
gen aut_out_lag0=autocracyFH_fill==0&autocracyFH_fill[_n-1]==1&countrycode==countrycode[_n-1] if year>=1992&year<=2018&autocracyFH_fill!=.
replace aut_out_lag0=0 if aut_out_lag0==1&(countrycode=="DJI"|countrycode=="GMB"|countrycode=="HND"|countrycode=="LBY"|countrycode=="MDV"|countrycode=="MRT") //<4 lags
replace aut_out_lag0=0 if aut_out_lag0==1&(countrycode=="BGD"|countrycode=="DOM"|(countrycode=="FJI"&year<2014)|countrycode=="KGZ"|countrycode=="MDA"|(countrycode=="MDG"&year<2014)|countrycode=="MOZ"|countrycode=="MWI"|countrycode=="PHL"|countrycode=="PRY"|countrycode=="RUS"|countrycode=="SYC"|countrycode=="UKR"|countrycode=="ZAF") //<4 leads
replace aut_out_lag0=0 if aut_out_lag0==1&(countrycode=="ALB"|countrycode=="CAF"|countrycode=="COG"|(countrycode=="GEO"&year<2012)|countrycode=="GNB"|countrycode=="GUY"|countrycode=="MLI"|countrycode=="NER"|countrycode=="ROU"|countrycode=="SLE"|countrycode=="THA"|countrycode=="TLS"|(countrycode=="BIH"&year==1996)|(countrycode=="HTI"&year==1994)|(countrycode=="LSO"&year==1993))	// short events in multi-event countries
list countryname year if aut_out_lag0==1 //list of countries for Table C3
*create leads and lags
forvalues x=1/25{
	local y=`x'-1
	gen aut_out_lag`x'=aut_out_lag`y'==0&aut_out_lag`y'[_n-1]==1&countrycode==countrycode[_n-1]&year==year[_n-1]+1 if year>=1992&year<=2018&autocracyFH_fill!=.
	replace aut_out_lag`x'=0 if autocracyFH_fill==1
}
gen aut_out_lead1=aut_out_lag0[_n+1]==1&aut_out_lag0==0&countrycode==countrycode[_n+1]&year==year[_n+1]-1 if year>=1992&year<=2018&autocracyFH_fill!=.
forvalues x=2/25{
	local y=`x'-1
	gen aut_out_lead`x'=aut_out_lead`y'==0&aut_out_lead`y'[_n+1]==1&countrycode==countrycode[_n+1]&year==year[_n+1]-1 if year>=1992&year<=2018&autocracyFH_fill!=.
	replace aut_out_lead`x'=0 if autocracyFH_fill==0
}
*create endpoint dummies at -9 or less and +9 or more
egen aut_out_lag9p=rowtotal(aut_out_lag9 - aut_out_lag25),missing
egen aut_out_lead9p=rowtotal(aut_out_lead9 - aut_out_lead25),missing
*interact leads and lags with lights
foreach x of varlist aut_out_lead9p aut_out_lead8 aut_out_lead7 aut_out_lead6 aut_out_lead5 aut_out_lead4 aut_out_lead3 aut_out_lead2 aut_out_lead1 aut_out_lag0 aut_out_lag1 aut_out_lag2 aut_out_lag3 aut_out_lag4 aut_out_lag5 aut_out_lag6 aut_out_lag7 aut_out_lag8 aut_out_lag9p{
	gen lndn18_`x'=lndn18_adj1417*`x'
}
*I create a dummy for autocracy observations not in these spells
egen temp=rowtotal(aut_out_lead1 aut_out_lead2 aut_out_lead3 aut_out_lead4 aut_out_lead5 aut_out_lead6 aut_out_lead7 aut_out_lead8 aut_out_lead9p),miss
gen autocracyFH_other=autocracyFH_fill-temp
gen lndn18_autocracyFH_other=lndn18_adj1417*autocracyFH_other
drop temp
*regression
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp_v2020_12 aut_out_lead9p aut_out_lead8 aut_out_lead7 aut_out_lead6 aut_out_lead5 aut_out_lead4 aut_out_lead3 aut_out_lead2 aut_out_lead1 aut_out_lag0 aut_out_lag1 aut_out_lag2 aut_out_lag3 aut_out_lag4 aut_out_lag5 aut_out_lag6 aut_out_lag7 aut_out_lag8 aut_out_lag9p autocracyFH_other lndn18_adj1417 lndn18_autocracyFH_other lndn18_aut_out_lead9p-lndn18_aut_out_lag9p if fiw!=., absorb(countrycode year) cluster(countrycode)
	return scalar sigma = _b[lndn18_autocracyFH_other]/_b[lndn18_adj1417]
exit
end
bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
estat bootstrap,bc p
mat a = e(b)
local sigma: di %4.3f a[1,1]
mat b = e(V)
local sigmaSE: di %4.3f sqrt(b[1,1])
reghdfe lngdp_v2020_12 aut_out_lead9p aut_out_lead8 aut_out_lead7 aut_out_lead6 aut_out_lead5 aut_out_lead4 aut_out_lead3 aut_out_lead2 aut_out_lead1 aut_out_lag0 aut_out_lag1 aut_out_lag2 aut_out_lag3 aut_out_lag4 aut_out_lag5 aut_out_lag6 aut_out_lag7 aut_out_lag8 aut_out_lag9p autocracyFH_other lndn18_adj1417 lndn18_autocracyFH_other lndn18_aut_out_lead9p-lndn18_aut_out_lag9p if fiw!=., absorb(countrycode year) cluster(countrycode)
parmest,saving(${temp}\autoc_out_event18,replace)
*draw graph
preserve
use ${temp}\autoc_out_event18,clear
keep in 24/40
gen id=0 in 9
replace id=id[_n-1]+1 in 10/17
replace id=[_n]-9 in 1/8
sort id
twoway (scatter estimate id,mcolor(green%70)) (rcap min95 max95 id), ytitle(Change in NTL elasticity of GDP) yline(0) ylabel(-0.1(0.05)0.15, angle(horizontal) format(%3.2f)) xtitle(Years since becoming Democracy) xlabel(-8(2)8) xline(0, lpattern(dash)) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) note(Other autocracy {&sigma}: `sigma' (`sigmaSE')) name(event_autoc_out18,replace)
graph export "${figures}\figure_A13b.pdf", as(pdf) replace
restore


///////////////////////////////////////////////////
////// Figure A14-A15: Adjusted GDP figures ////////
///////////////////////////////////////////////////

*Produced above with Figure 6

////////////////////////////////////////////
////// Figure A16: SDDN event study ////////
////////////////////////////////////////////

*Produced above with Table 6

/////////////////////////////////////////////////
////// Figure C1: Montecarlo simulations ////////
/////////////////////////////////////////////////

*Produced by do-file "Montecarlo simulations"

//////////////////////////////////////////////////////////////////////
////// Figure D1: Disaggregate measures of statistical capacity //////
//////////////////////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

*bootstrap program
capture program drop sigma_boot
program define sigma_boot, rclass
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw lndn13_x, absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	sum x if e(sample)
	local x_mean: di %3.2f r(mean)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/(_b[lndn13]+(`x_mean'*_b[lndn13_x]))
exit
end

*loop over all measures (I dropped mortality [mortal] because it is 1 for everyone)
local count=1
foreach x in agcen excncpt exdebt fscov gdp gender hiv hlthsurv immun impexp indust malnut matern nabase popcen popreg poverty povsurv prcpbase primcomp sdds unesco water who{
	gen x=sci_`x' 
	gen lndn13_x=lndn13*x
	bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot
	estat bootstrap,bc p
	parmest,saving(${temp}\stats_`x',replace) idstr(`x') erows(ci_percentile ci_bc)
	parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(FD1C`count') erows(ci_percentile ci_bc)
	local i=`i'+1
	local count=`count'+1
	drop x lndn13_x
}

*draw graph
preserve
	clear
	foreach x in agcen excncpt exdebt fscov gdp gender hiv hlthsurv immun impexp indust malnut matern nabase popcen popreg poverty povsurv prcpbase primcomp sdds unesco water who{
		cap append using ${temp}\stats_`x'
	}
	gen id=_n
	lab define char ///
	1	"Agricultural census" ///
	2	"BOP manual in use" ///
	3	"External debt reporting status" ///
	4	"Government finance accounting" ///
	5	"Per capita GDP growth" ///
	6	"Gender equality" ///
	7	"HIV/AIDS" ///
	8	"Health survey" ///
	9	"Immunization" ///
	10	"Import and export price indexes" ///
	11	"Industrial production index" ///
	12	"Child malnutrition" ///
	13	"Maternal health" ///
	14	"National accounts base year" ///
	15	"Population census" ///
	16	"Vital registration system coverage" ///
	17	"Income poverty" ///
	18	"Poverty survey" ///
	19	"Consumer price index base year" ///
	20	"Primary completion" ///
	21	"Special Data Dissemination Standard" ///
	22	"UNESCO reporting" ///
	23	"Access to water" ///
	24	"National immunization coverage"

	label values id char

	twoway (scatter estimate id,mcolor(navy)) (rcap er_2_1 er_2_2 id), ylabel(,angle(horizontal)) xlabel(1(1)24, angle(45) valuelabel labsize(small)) ytitle({&sigma} estimate) xtitle("") legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(stats_full,replace) 
	graph export "${figures}\figure_D1.pdf", as(pdf) replace
restore

//////////////////////////////////////////////////////////////////
////// Figure D2: Subperiods with Li et al data until 2018 ///////
//////////////////////////////////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

gen lndn=lndn18_adj1417
gen lndn_fiw=lndn*fiw

capture program drop sigma_boot
program define sigma_boot, rclass
	args x y
	reghdfe lngdp_v2019_12 lndn fiw fiw2 lndn_fiw if year>=`x'&year<=`y', absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn_fiw]/_b[lndn]
exit
end

local count=0
forvalues yr=0/13{
	local start=1992+`count'
	local end=2005+`count'
	bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed(${seed}): sigma_boot `start' `end'
	estat bootstrap,bc p
	parmest,saving(${temp}\window_`start'_`end',replace) erows(ci_percentile ci_bc)
	local count=`count'+1
}

clear
local count=0
forvalues yr=0/13{
	local start=1992+`count'
	local end=2005+`count'
	append using ${temp}\window_`start'_`end'
	local count=`count'+1
}
gen start=1991+[_n]
gen end=2004+[_n]

twoway (scatter estimate start,mcolor(navy)) (rcap er_2_1 er_2_2 start), ytitle(Estimated {&sigma}) yline(0) ylabel(0(0.5)1.5,angle(horizontal)) xtitle(Start/End Year) xlabel(1992 "92-05" 1993 "93-06" 1994 "94-07" 1995 "95-08" 1996 "96-09" 1997 "97-10" 1998 "98-11" 1999 "99-12" 2000 "00-13" 2001 "01-14" 2002 "02-15" 2003 "03-16" 2004 "04-17" 2005 "05-18", angle(forty_five)) legend(off) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(subperiods,replace)
graph export "${figures}\figure_D2.pdf", as(pdf) replace

/////////////////////////////////////
////// Figure D3: WDI vintages //////
/////////////////////////////////////

use "${dir}\Data\master\Estimations", clear

*bootstrap program
capture program drop sigma_boot
program define sigma_boot, rclass
	args yr mo
	reghdfe lngdp_v`yr'_`mo' lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end

*run regressions
local count=1
forvalues x=2014/2021{
	foreach y in 01 02 03 04 05 06 07 08 09 10 11 12{
		cap reghdfe lngdp_v`x'_`y' lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
		sum fiw if e(sample),det
		local temp: di r(N)
		if `temp'>0{
			bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot `x' `y'
			estat bootstrap,bc p
			parmest,saving(${temp}\WDI_`x'_`y',replace) idstr(`x'_`y') erows(ci_percentile ci_bc)
			parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(FD3C`count') erows(ci_percentile ci_bc)
			local i=`i'+1
			local count=`count'+1
		}
	}
}
*create dataset
clear
forvalues x=2014/2021{
	foreach y in 01 02 03 04 05 06 07 08 09 10 11 12{
		cap append using ${temp}\WDI_`x'_`y'
	}
}
*draw graph
gen day=date(idstr, "YM")
gen month=mofd(day)
format month %tm
twoway (scatter estimate month,mcolor(navy)) (rcap er_2_1 er_2_2 month), ylabel(,angle(horizontal)) xlabel(, angle(45) format(%tmMon_CCYY)) ytitle({&sigma} estimate) xtitle(WDI Vintage) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(wdi_vintage,replace) 
graph export "${figures}\figure_D3.pdf", as(pdf) replace

//////////////////////////////////////////////////////////////////
////// Figure D4: Excluding subregions, or 1 or 2 countries //////
//////////////////////////////////////////////////////////////////

////////////Excluding subregions

use "${dir}\Data\master\Estimations", clear

capture program drop sigma_boot
program define sigma_boot, rclass
	args excl
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if subsubregcode_un!=`excl', absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end

local count=1
levelsof subsubregcode_un,local(sub)
foreach x of local sub{
	bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot `x'
	estat bootstrap,bc p
	parmest,saving(${temp}\drop_r`x',replace) erows(ci_percentile ci_bc)
	parmest,saving(${sigmas}\sigma`i',replace) idnum(`i') idstr(FD4C`count') erows(ci_percentile ci_bc)
	local i=`i'+1
	local count=`count'+1
}

preserve
	clear
	foreach x of local sub{
		append using ${temp}\drop_r`x'
	}

	gen subregion=_n

	label define sub ///
	1 "South America" /// (5)
	2 "Western Africa" /// (11)
	3 "Central america" /// (13)
	4 "Eastern Africa" /// (14)
	5 "Northern Africa" /// (15)
	6 "Middle Africa" /// (17)
	7 "Southern Africa" /// (18)
	8 "Northern America" /// (21)
	9 "Caribbean" /// (29)
	10 "Eastern Asia" /// (30)
	11 "Southern Asia" /// (34)
	12 "Southeastern Asia" /// (35)
	13 "Southern Europe" /// (39)
	14 "Australia and NZ" /// (53)
	15 "Melanesia" /// (54)
	16 "Micronesia" /// (57)
	17 "Polynesia" /// (61)
	18 "Central Asia" /// (143)
	19 "Western Asia" /// (145)
	20 "Eastern Europe" /// (151)
	21 "Northern Europe" /// (154)
	22 "Western Europe" // (155)
	label values subregion sub

	twoway (scatter estimate subregion) (rcap er_2_1 er_2_2 subregion), ytitle(Estimated {&sigma}) ylabel(, angle(horizontal)) xtitle(Excluded subregion) xlabel(1(1)22, labsize(vsmall) angle(forty_five) valuelabel) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(exclusions,replace)
	graph export "${figures}\figure_D4c.pdf", as(pdf) replace
restore

///////////Excluding one country

use "${dir}\Data\master\Estimations", clear

global ctrys "${dir}\Data\temp\ctrys"

capture program drop sigma_boot
program define sigma_boot, rclass
	args excl
	reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if countrycode_num!=`excl', absorb(countrycode year) cluster(countrycode)
	sum fiw if e(sample),det
	local iqr: di %2.1f r(p75)-r(p25)
	return scalar sigma = `iqr'*_b[lndn13_fiw]/_b[lndn13]
exit
end

*determine set of countries in main sample (T1C4) 
reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
levelsof countrycode_num if e(sample), local (ctrys)

foreach x of local ctrys{
	bootstrap r(sigma), cluster(countrycode) idcluster(ctrycode) reps(${reps}) seed($seed): sigma_boot `x'
	estat bootstrap,bc p
	parmest,saving(${ctrys}\ctry`x',replace) idnum(`x') erows(ci_percentile ci_bc)
}

preserve
	clear
	foreach x of local ctrys{
		append using ${ctrys}\ctry`x'
	}
	save ${temp}\ctrys.dta,replace

	use ${temp}\ctrys.dta,clear
	sort estimate
	gen id=_n
	twoway (scatter estimate id, msymbol(circle) msize(vsmall)) (rcap er_2_1 er_2_2 id), yline(0) ylabel(, angle(horizontal)) ytitle({&sigma} estimate) xtitle(Regression) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) yline(0.354, lpattern(dash) lcolor(red)) name(ctrys,replace)
	graph export "${figures}\figure_D4a.pdf", as(pdf) replace
restore

///////////Excluding two countries

use "${dir}\Data\master\Estimations", clear

global ctrys2 "${dir}\Data\temp\ctrys2"

reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw, absorb(countrycode year) cluster(countrycode)
// I restrict the sample to countries and years used in the estimation
keep if e(sample)
// I create a new numerical ID for the remaining sample that goes from 1 to 184 (key: no holes)
egen id=group(countrycode)

forvalues x=1/183{
	local k=`x'+1
	forvalues y=`k'/184{
		reghdfe lngdp14 lndn13 fiw fiw2 lndn13_fiw if id!=`x'&id!=`y', absorb(countrycode year) cluster(countrycode)
		sum fiw if e(sample),det
		local iqr: di %2.1f r(p75)-r(p25)
		nlcom sigma: `iqr'*_b[lndn13_fiw]/_b[lndn13],post
		parmest,saving(${ctrys2}\ctry`x'_`y',replace) idstr(`x'_`y')
	}	
} 

preserve
	clear
	forvalues x=1/183{
		local k=`x'+1
		forvalues y=`k'/184{
			append using ${ctrys2}\ctry`x'_`y'
		}
	}
	save ${temp}\ctrys2.dta,replace

	use ${temp}\ctrys2.dta,clear

	sort estimate
	gen id=_n
	sum estimate,det
	local mean: di %3.2f r(mean)
	local median: di %3.2f r(p50)
	twoway (scatter estimate id, msymbol(circle) msize(vsmall)) (line max95 id) (line min95 id), yline(0) ylabel(, angle(horizontal)) ytitle({&sigma} estimate) xtitle(Regression) xlabel(0(5000)15000) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) yline(0.354, lpattern(dash) lcolor(red)) name(ctrys2,replace)
	graph export "${figures}\figure_D4b.pdf", as(pdf) replace
restore

//////////////////////////////////////////////////////
////// Figure D5: 1% subsamples of ZAMinfluence //////
//////////////////////////////////////////////////////

*Produced above with Table D16

//////////////////////////////////////
////// Figures 3 and D6: Sigmas //////
//////////////////////////////////////

*create sigma dataset
clear
local j=`i'-1
forvalues x=1/`j'{
    append using ${sigmas}\sigma`x'
}
save ${temp}\sigmas.dta,replace

use ${temp}\sigmas.dta,clear

*graph of estimates
sort estimate
gen id=_n

*Sigma 94 (TD14C7) estimated w/ Delta method
replace er_1_1=min95 if er_1_1==.&min95!=.
replace er_2_1=min95 if er_2_1==.&min95!=.
replace er_1_2=max95 if er_1_2==.&max95!=.
replace er_2_2=max95 if er_2_2==.&max95!=.

sum estimate,det
local mean: di %3.2f r(mean)
local median: di %3.2f r(p50)

local trim = 3
gen er_2_2_tr=cond(er_2_2>`trim',`trim',er_2_2)
gen er_1_2_tr=cond(er_1_2>`trim',`trim',er_1_2)

gen min95_tr=cond(min95<-`trim',-`trim',min95)
gen max95_tr=cond(max95>`trim',`trim',max95)

// Bias-corrected CI
twoway (rcap er_2_1 er_2_2 id if er_2_2<`trim',color(gs10)) (rcap er_2_1 er_2_2_tr id if er_2_2>=`trim',lpattern(dash) color(gs10)) (scatter estimate id, msymbol(circle) msize(vsmall) mcolor(gs5)), yline(0) ylabel(, angle(horizontal)) ytitle({&sigma} estimate) xtitle(Regression) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) yline(`mean', lpattern(dash) lcolor(gs8)) name(sigmas_bc,replace)
graph export "${figures}\figure_3.eps", as(eps) replace

//Normal
twoway (rcap min95 max95 id if min95>-`trim'&max95<`trim',color(gs10)) (rcap min95_tr max95_tr id if min95<=-`trim'|max95>=`trim',lpattern(dash) color(gs8)) (scatter estimate id, msymbol(circle) msize(vsmall) mcolor(navy)), yline(0) ylabel(-3(1)3, angle(horizontal)) ytitle({&sigma} estimate) xtitle(Regression) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) yline(`mean', lpattern(dash) lcolor(red)) name(sigmas_norm,replace)
graph export "${figures}\figure_D6a.pdf", as(pdf) replace

//Percentile CI
twoway (rcap er_1_1 er_1_2 id if er_1_2<`trim',color(gs10)) (rcap er_1_1 er_1_2_tr id if er_1_2>=`trim',lpattern(dash) color(gs8)) (scatter estimate id, msymbol(circle) msize(vsmall) mcolor(navy)), yline(0) ylabel(, angle(horizontal)) ytitle({&sigma} estimate) xtitle(Regression) legend(off) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) yline(`mean', lpattern(dash) lcolor(red)) name(sigmas_pct,replace)
graph export "${figures}\figure_D6b.pdf", as(pdf) replace

timer off 1
timer list 1 